Im working on a kinda script that would make a guy follow you until you cant be seen, he then would go to the last point you where seen and then if it cant see you again it returns to where it was. Well when it sees you it follows you and when you go out of site it moves to the last point you where seen, also chasing you if you ever come into sight again, But for some reason my line "if(Vector3.Distance(transform.position, LastPosition) < 4)" doesnt work (quotes are not part of it). The NPC moves to the last seen position, and just rotates moving around it, i tried increasing the value of 4, even when i set it to 100 it doesnt work... Any explination?
Full Script Here
var speed = 3.0;
var rotateSpeed = 3.0;
var Target: Transform;
var StartPoint: Transform;
var wheel1: Transform;
var wheel2: Transform;
private var CanSee: boolean = false;
private var LastPosition;
private var pos;
private var move = false;
private var lastseen=0;
function Update ()
{
var hit : RaycastHit;
Physics.Linecast (transform.position, Target.position, hit);
if(hit.transform.tag == "Player"){
lastseen = 0;
move = true;
pos = Target.position;
LastPosition = Target.position;
} else {
if(CanSee){
if(lastseen == 0){
pos = LastPosition;
if(Vector3.Distance(transform.position, LastPosition) < 4){
lastseen = 1;
}
}
if(lastseen == 1){
pos = StartPoint.position;
if(Vector3.Distance(transform.position, StartPoint.position) < 10){
CanSee = false;
move = false;
lastseen = 0;
}
}
} // can see if statement ends here
}
if(move){
Attack(pos);
if(wheel1){
wheel1.SendMessageUpwards("PlayAnimation");
}
if(wheel2){
wheel2.SendMessageUpwards("PlayAnimation");
}
}
}
function Attack(pos : Vector3){
print(pos);
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
var rotation = pos - transform.position;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(rotation), rotateSpeed * Time.deltaTime);
transform.rotation.x = 0;
transform.rotation.z = 0;
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
}
@script RequireComponent(CharacterController)