unity 2d rotate towards mouse only z code example

Example 1: unity 2d rotate towards direction

/// <summary>
    /// Rotate a gameobject to face a direction in 2D space with offet
    /// </summary>
    /// <param name="target"></param>
    /// <param name="RotationSpeed"></param>
    /// <param name="offset"></param>
    private void RotateGameObject(Vector3 target, float RotationSpeed, float offset)
    {
        //https://www.youtube.com/watch?v=mKLp-2iseDc
        //get the direction of the other object from current object
        Vector3 dir = target - transform.position;
        //get the angle from current direction facing to desired target
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        //set the angle into a quaternion + sprite offset depending on initial sprite facing direction
        Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
        //Roatate current game object to face the target using a slerp function which adds some smoothing to the move
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, RotationSpeed * Time.deltaTime);
    }

Example 2: unity rotate towards mouse

using UnityEngine;     using System.Collections;          public class SCR_mouseRotation : MonoBehaviour {                  void Update () {             Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);             Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);             lookPos = lookPos - transform.position;             float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;             transform.rotation = Quaternion.AngleAxis(angle, Vector3.down); // Turns Right                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.up); //Turns Left                      }     }