//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);}
}
This is javascript rather than c#. Make sure this lives inside a .js file.
Your variable declarations were all commented out.
Here’s the fixed code. I also cleaned up the tabs a little bit.
//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);}
}