Copying the players movements

Hi, so i want my enemy to follow the player at a distance and stop when the player looks at him

I am using this code

var target : Transform; //the enemy's target

var moveSpeed = 3; //move speed

var rotationSpeed = 3; //speed of turning



var myTransform : Transform; //current transform data of this enemy



function Awake()

{

    myTransform = transform; //cache transform data for easy access/preformance

}



function Start()

{

     target = GameObject.FindWithTag("Player").transform; //target the player



}



function Update () {

    //rotate to look at the player

    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);



    //move towards the player

    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;





}

and i also want the enemy to teleport away and a screen effect to occour (eg BLUR) when the player looks at it for more than 10s

thankyou

fyi: my skype name is rubenweir_ if you need to contact me personally

Looks like you need to set conditional statements

boolean are nice , but I am a big fan of using interger to set AI states (eg)

public var aiState : int;

function Update() {
    if(aiState==1) {
    print("Current AI State = " + aiState + " @ " + name);
    }
    else if(aiState==2) {
    Debug.Log("Do some cool stuff" @ " + name);
    }
}

You really don’t want to use if-statements to check the states. That’s what state machines are for. The better way do it is by applying the Finite State Machine pattern, where each pattern is stored in it’s own class file. This clearly separates every state and it’s associated code and is very clean and easy to maintain.

If you put everything into one single file, you will soon get spaghetti code which becomes a maintenance hell.