How to rotate a 2D object to rotate towards the position of another object?

0

I present the following problem, I have an enemy that I want to turn towards the position where the player is, but I do not know that making the npc object knows which side to look at, the game in question is as if it were mario bros and not a top down game. my question is:

What method do you know to make the npc know which direction to look at?

    
asked by NessMastert_Ration 27.04.2016 в 06:11
source

1 answer

2

A typical solution in 2d:

Vector3 direccion = player.position - transform.position;
float angulo = Mathf.Atan2(direccion.y, direccion.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angulo, Vector3.forward);
    
answered by 27.04.2016 в 15:41