W
W
Wadim_wadim20002020-05-14 15:04:16
C++ / C#
Wadim_wadim2000, 2020-05-14 15:04:16

Unity - how to make a uniform rotation towards the cursor?

I have a script for a 2d game, and this part of the script is responsible for turning the tank's turret:

Quaternion TurretRotation()
  {
    Vector3 mouse = Input.mousePosition;
    mouse.z = Camera.main.transform.position.z;
    Vector3 direction = Camera.main.ScreenToWorldPoint(mouse) - transform.position;
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    return Quaternion.AngleAxis(angle, Vector3.forward);
  }

why, the closer it turns to the desired point, the slower it does it? How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BrightOnX, 2020-05-14
@BrightOnX

try this code
i recently discovered turns myself, maybe it will help

Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

         difference.Normalize();

         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

         transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question