Enemy Movement Help

I am working on a topdown 2D game and was testing it when I saw that the enemies that spawned went to where the player was placed before it started moving. My code for the enemy movement is `using UnityEngine;
using System.Collections;

public class EnemyMotion : MonoBehaviour {

public float speed;
public Transform player;

void FixedUpdate()
{

	float z = Mathf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90;

	transform.eulerAngles = new Vector3 (0, 0, z);

	rigidbody2D.AddForce (gameObject.transform.up * speed);

}

}
`

Please Help

I don’t really see why it’s a problem if the enemies follow the player when the player isn’t moving. Maybe you meant the player wasn’t active?

public GameObject player;

void FixedUpdate()
{
    if(player.activeInHierarchy)
    {
        //follow player

    } 
}

public Transform player;

alone is not enough to retrieve updated information.

Your enemy will, in Start(), require to ‘Find’ the player Object and you will need to GetComponent(Transform) the result.

Any subsequent call to the players information will herald updated information.

Change “public Transform player” to “public GameObject player”. You want the enemies to follow the player’s current position, not where he was before the game even starts.