Follow a game object?

Hello. I am trying to make a script that makes the enemies follow the player in my game. When I put in this code I get the error

What can I change in my script to make it work?

var player = Transform;
var direction : Vector3 = (player.position - transform.position);

function Update(){
direction.Normalize();
transform.position += direction * 40 * Time.deltaTime;
}

transform is a different thing to Transform.
where is your transform referring to? how do you call the code? is it attached to a gameObject?

You’ll probably just want to use Transform.Translate(vector3.forward * 40 * Time.deltaTime); Then you could either use Transform.Rotate to rotate towards the player or you could just use Transform.LookAt(player) which will keep the enemy constantly looking towards the player.

‘direction’ should be declared inside Update(). Also, it looks like the syntax is wrong in the first line. (There are other ways this can be done as well, as noted above.)

You can use:

 MyObject.transform.position = Vector3.Lerp(ObjectToFollow.transform.position,MyObject.transform.position,FollowSpeed*Time.deltaTime);

cheers

I wouldn’t recommend this as a general solution. (Among other things, the speed of the following object will be dependent on the distance between the two objects. If that’s the desired behavior then the above solution may be suitable, but more often than not, I think, that’s not the desired behavior.)