Lookat player

Hi there

I am making a 2D top down game. I am now trying to make the enemies always rotate to face the player. Ive just tried this:

using UnityEngine;
using System.Collections;

public class ZLookatPlayer : MonoBehaviour {
	
	public Transform Player;

	void Update () {

		    // Rotate the camera every frame so it keeps looking at the Player
			
			transform.LookAt(Player);

	}
}

With no success. It seems like the zombies rotates on all 3 axis, making them invisible when vivewing from a 2d perspective. I guess the lookat is only for 3d… Anyway, could anybody help me with a usefull method to make this work in a 2d top-down environment? I have viewed atleast 4 similar threads with no successs.

Thank you in advance!

Try with this :

Vector3 direction = Player.position - transform.position ;

// Prevent weird behavior if the player and enemy are very close to each other
if( direction.magnitude > 0.1f )
{
    // If the target is at the right side of the enemy
    if( Vector3.Dot( Vector3.right, direction ) > 0 )
        transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, -90, 0 ) ;
    // If the target is at the left side of the enemy, prevent the sprite to flip
    else
        transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, 90, 180 ) ;

}

Not familiar with 2D too but try this:

transform.LookAt(new Vector3(Player.position.x, transform.position.y, Player.position.z));

Not entirely sure about the axis being right…