- Home /
Rotate around 0,0 and face 0,0
I have my boss rotating around 0,0 but I also need him to face towards 0,0. This is the part that isn't work. He doesn't face towards 0,0 but instead away from it.
https://gyazo.com/fb2f0d4f3a3bc861e2ff9d48688b4bb5
This is the code that I am using for the circular movement and him facing towards 0,0
// Points towards (0,0)
Vector3 difference = Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, transform.position.z)) - transform.position;
difference.Normalize();
float rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotation);
// Move
timeCounter += Time.deltaTime;
float x = Mathf.Cos(timeCounter) * radius;
float y = Mathf.Sin(timeCounter) * radius;
transform.position = new Vector3(x, y, transform.position.z);
Answer by Bunny83 · Nov 26, 2017 at 07:31 PM
Uhm, currently your boss is facing the lower left corner of the screen because that's what you passed in. In Screen space 0,0 is the bottom left corner and (Screen.width, Screen.height) is the top right corner. If you want the center of the screen you would need to pass in half the screen resolution. However it would be easier to use ViewportToWorldPoint and just pass (0.5f, 0.5f) as position. Viewport space is basically the same as screen space but in the range 0 - 1 and not 0 - screensize.
Also you may notice that your "facing direction" is 90° off. This is because mathematically 0° is right. So you probably want to add 90 to your "rotation".
Finally it would make more sense to first update the position and then calculate the rotation based on the position. At the moment you use the last position to rotate your boss and then move it. So it would be slightly off (one frame off).