- Home /
Why Rotate player toward mouse direction keeps increasing rotation speed?
I have a top-down 2D game. My player moves towards my mouse position on screen. I want my player to rotate smoothly when mouse position changes. I'm doing this:
void Update(){
Vector3 mouse = Input.mousePosition;
Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
Vector2 offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
Float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.Euler(0, 0, angle),Time.time);
}
when I go play, the player rotates smoothly everytime the mouse change position, but after a couple of seconds it starts to rotate faster everytime until is not an smooth rotation anymore. I want the speed rotation to be the same everytime. Dont know what is going on.
Answer by ninja_gear · Jun 02, 2016 at 08:44 PM
You are making the transform rotate based on the time from the start up of the program. You need to make it rotate based on time since the last frame. Change the last argument passed to RotateTowards() from Time.time to Time.deltaTime:
transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.Euler(0, 0, angle),Time.deltaTime);
Your answer
Follow this Question
Related Questions
Function to rotate my Player smoothly taking the y and use it to have angle 1 Answer
How to rotate object slowly on only Z axis 2 Answers
Is there a better way to do rotations with new Unity 2D? 1 Answer
I need help with clamping a rotation towards the mouse position 2 Answers
Player Arm will not rotate when moving with Xbox Joystick 1 Answer