Hey there! I have a main character whos running away from a policeman and so far its working quite well.. What I want to do now is the policeman not being able to chase the player until he starts running (because at the moment the player doesnt have time to prepare himself to run away from him)... Any ideas?
This is the script im using rightnow....
var enemy: Transform;
var player: GameObject;
var dir: Vector3;
var speed: float;
var observed: boolean;
function Update () {
if (observed) {
dir = player.transform.position - transform.position;
dir = dir.normalized;
transform.Translate(dir * speed, Space.World);
}
else {
transform.eulerAngles.y = Mathf.PingPong(/*Time.time*/20,90) -45;
}
}
function OnTriggerEnter(other: Collider){
if(player) observed = true;
}
If you want to wait until the player starts running away, store another boolean variable for whether the policeman is chasing or not. Then, in your update when you check whether observed is true or not, also check whether he's chasing. If he is, do what you're currently doing. If he's not, then check to see if your player's speed is over a certain threshold and if it is, then set chasing to true. In the following code, you'll need to define chaseSpeed. I'm assuming you're using a character controller and that's how the player's speed is being retrieved. If not, you'll need to replace player.GetComponent("CharacterController").velocity with whatever method you like for determining the player's speed.
function Update () {
if (observed) {
if(chasing) {
dir = player.transform.position - transform.position;
dir = dir.normalized;
transform.Translate(dir * speed, Space.World);
}
else if(player.GetComponent("CharacterController").velocity > chaseSpeed)
{
chasing = true;
}
}
else
{
transform.eulerAngles.y = Mathf.PingPong(/*Time.time*/20,90) -45;
}
}