unity 2d look at object code example
Example 1: 2d object look at object
Vector3 dir = enemy.position - transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Example 2: 2d look at unity
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);
}
}
Example 3: unity look at 2d
transform.right = target.position - transform.position;