The question is answered, right answer was accepted
Align an object rotation in relation to another object
Hello. Excuse me for any grammatical errors. I'm making a 2D game in unity and don't know how to fix this problem.
In this game, there is a ball at the center of the screen and every x seconds an object spawns at the border of the screen and it goes to the ball. This is a pic that can clarify what my problem is: I just would like to rotate every object toward the ball . Any ideas?
P.S: I have already tried many solution that I found on the net, it worked but the problem was that these solutions were editing also the x and y components of the rotation making the image disappear (because it is a 2d game so if I edit the x component of the rotation to 90° it seems like to disappear).
Thank you!
---- FIXED ----
I found a great solution:
Vector3 difference = target.position - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);