How can I rotate a gameObject around Z axis to face another gameObject?

Hi everybody.
I’m making a new 2D Android game something like Flappy Bird.

I have a fish (the player) and the shark (an enemy) and I want the shark to rotate around Z axis to face the fish.
I tried this:

function Update () {
	var lookPos = pesce.transform.position - transform.position;
	lookPos.x = 0;
	lookPos.y = 0;
	var rotation = Quaternion.LookRotation(lookPos);

	transform.rotation = Quaternion.Slerp(this.transform.rotation,rotation, Time.deltaTime * 2);

I tried transform.LookAt(fish), too, but it still doesn’t work :confused:

But it doesn’t work.
How can I do it?
Sorry for may bad English and thanks in advance :slight_smile:

Hi, you can earn Z rotaion in 2D using Mathf.Atan2() like this:

        private void Update()
            // Get Angle in Radians
            float AngleRad = Mathf.Atan2(Target.transform.position.y - Entity.transform.position.y, Target.transform.position.x - Entity.transform.position.x);
            // Get Angle in Degrees
            float AngleDeg = (180 / Mathf.PI) * AngleRad;
            // Rotate Object
            this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

You can do like this:

public GameObject fish;
    void Update () {
    		//keep the z position of the gameobject, LookAt the x and y position
    		Vector3 LookPos = new Vector3(fish.transform.position.x,fish.transform.position.y,transform.position.z);
    		gameObject.transform.LookAt (LookPos);

Make sure the the local blue axis of the shark points forward.