Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- /*
- COPYRIGHT 2012 HARRISON TURTON, RELEASED UNDER CREATIVE COMMONS, USEAGE ALLOWED IF YOU CREDIT HARRISON TURTON
- */
- //target to follow
- var target : Transform;
- //Checks his position, used to follow the target
- var pos : Transform;
- //Ray variables (Length... etc.)
- var rayLength : float = 3;
- //Movement, speed etc.
- var speed : float = 2;
- //You can move if he is not being looked at
- var move : boolean = false;
- //I had problems with my model sinking into the floor, adjust this variable if you need it, or remove it
- function FixedUpdate()
- {
- transform.position.y = 2;
- }
- function Update()
- {
- //Setting up Raycast variables for simple object avoidance
- var fwd = transform.TransformDirection (Vector3.forward);
- var hit : RaycastHit;
- //If you are looking at the object...
- if (renderer.isVisible)
- {
- move = false;
- }
- //If you are NOT looking at the object...
- if(!renderer.isVisible)
- {
- move = true;
- }
- //If you are not looking at the object...
- if(move)
- {
- //Make him look at the target
- transform.LookAt(target);
- //Always follow the target
- pos.position += pos.forward * speed * Time.deltaTime;
- }
- //If he is 3 units away from something, move right (Works if you are not looking at the object)
- if (Physics.Raycast (transform.position, fwd, rayLength) && move)
- {
- Debug.Log("Something ahead, moving");
- transform.Translate(Vector3.right * 3 * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement