- Home /
How can I Lerp the Rotation of my Game Object to my Mouse Pointer?
Hi, I have just started programming a top down shooter game and I have almost set up the player character. All there is to do now is to make the character Lerp to face the direction of the mouse relative to the character's current position. I have written this script, taken from a few other peoples questions on Unity Answers, which I think should work so I don't understand why it doesn't.
// Create target rotation facing mouse pointer
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
targetRotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);
// Lerp to target rotation
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.time * turnSpeed);
As you can see by the first section of code, my target rotation is set to face the mouse pointer. Then in the second section of code you can see I try to lerp my transform.rotation to the target rotation. It does definitely rotate to face the mouse but instead of lerping it to that rotation all the time, it starts off lerping it slowly then gradually speeds up the lerp speed over time until it becomes impossible to even notice the lerp. If anyone can help me it world be much appreciated.
Many Thanks,
Tommy Eaves
Answer by Fair-Strides · Aug 07, 2016 at 03:32 PM
Have you tried replacing "Time.time" with "Time.deltaTime"?
Your answer
Follow this Question
Related Questions
Mouse controls sometimes "bounces" 3 Answers
(s)lerp y-rotation to the direction of velocity 0 Answers
Adjust camera rotation while looking at a target 1 Answer
Rotation resets? 1 Answer
Uniform rotation - uniform lerp 1 Answer