- Home /
I need help with Enemy following my Player
Hello everyone, this is kind of a specific question, so will try to clarify as much as I can. I have a 2D top shooter game, where I instantiate Enemies from different points and they follow my Player until they touch him and kill him. I already have set colliders, spawning points and the wave spawner and the enemies do follow properly the Player, but only when they already start in the game itself.
When I instantiate them they all start without the variable referenced that they have to specifically follow the player(its transform). So I made a prefab of the player and made them reference that. However now the enemies that I instantiate they go towards the original Transform point where my Player started and thats it, they never keep on looking for the new position the Player has, only the original position when game started.
The script for the enemy and its movement is the following:
public class Enemy : MonoBehaviour {
public Transform player;
public float moveSpeed = 4f;
private Vector2 movement;
private Rigidbody2D rbEnemy;
// Start is called before the first frame update
void Start()
{
rbEnemy = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
rbEnemy.rotation = angle;
direction.Normalize();
movement = direction;
}
private void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector2 direction)
{
rbEnemy.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(GameObject.FindGameObjectWithTag("Player"));
}
}
}
Answer by iveL_newO · May 07, 2020 at 04:54 PM
It looks like you're setting the player transform variable in the editor. This would mean that instantiated versions don't have that set (unless you mess with the prefab I believe). A way to get around this would be to searching for the player when an enemy is instantiated like this:
player = Gameobject.Find("Your player name").transform;
This isn't optimal, however, because you're going to be searching a whole lot which isn't great for performance. Instead, you could store the player's transform in the script you're using to instantiate enemies. Then, whenever you instantiate an enemy you could modify the public player variable on your new enemy's script. So you would add a variable called "player" to the script instantiating enemies:
Public Transform player;
Then instantiate your enemies like this:
Instantiate(EnemyPrefab) as GameObject.getComponent<Enemy> ().player = player;
(If you're already storing the instantiated object in the script already then just use everything after .getComponent)
thanx! I actually went with the first fix, I kinda had something like that in $$anonymous$$d, but it is just for a small game so performance was fine.