unity look at object 2d code example

Example 1: look at Unity 2D

Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;        
diff.Normalize();          
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;         
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);

Example 2: 2d look at unity

//Look at 2D job using the unity jobs system
struct LookAtJob : IJobParallelForTransform
{
    public NativeArray<Vector2> targetPositions;
    public void Execute(int index, TransformAccess transform)
    {
        Vector2 dir = targetPositions[index] - (Vector2)transform.position;
        float angle = math.degrees(math.atan2(dir.y, dir.x));
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    }
}