Hello. I am trying to make the enemies in my game follow the player. This is my script, but it is not working, what can be done to fix it? Thanks
function Update(){
transform.Translate(GameObject.FindWithTag("Player").transform.position; * 40);
}
Hello. I am trying to make the enemies in my game follow the player. This is my script, but it is not working, what can be done to fix it? Thanks
function Update(){
transform.Translate(GameObject.FindWithTag("Player").transform.position; * 40);
}
You don't want to be searching by tag on a per-update basis if you can avoid it. Instead, grab a reference to the player game object or transform in Start() or Awake() and just use that.
Your code has a syntax error in it, so it's not going to compile anyway. (As such, I'm not sure how you've determined whether it's working or not :)
Translating by the player's position is unlikely to do anything meaningful.
There are several ways you can get one object to follow another. Here's one (untested):
Vector3 direction = player.position - transform.position;
direction.Normalize();
transform.position += direction * speed * Time.deltaTime;
Note that this assumes the positions of the two objects will never be the same or nearly the same.