- Home /
How to smoothly rotate object in direction of analog joystick?
Hi guys, I am making a top down 2d game and am trying to get my object to smoothly rotate to whatever direction the analog joystick is facing. Right now I have it where it will face whichever direction the joystick is, but I don't want it to be able to flip. I want it to turn normally. Here is the code I have right now.
Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical"), 4096);
if (lookVec.x != 0 && lookVec.y != 0)
transform.rotation = Quaternion.LookRotation(lookVec, Vector3.back);
Is there some way I can just modify this code, or will I have to try something a little different? Thanks! :)
Answer by Kauppasarvi · Jun 16, 2017 at 09:18 PM
@jobo22 Hello, you could use Quaternion.Lerp, like this:
Quaternion targetRotation = Quaternion.LookRotation (lookVec, Vector3.back);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, Time.deltaTime);
Also, here is an article for further reading, not mine however.
@$$anonymous$$auppasarvi Your code works exactly how I wanted! Thank you very much for the quick reply! :)
This is the smoothest code for rotation so far I have seen, but it still has small deadzones where the game object stops for a small amount of degrees in NORTH, SOUTH, EAST, WEST, NORTH-EAST, NORTH WEST, SOUTH-EAST and SOUTH-WEST. Is there any way for a complete 360 degree smoothness? Imagine a 2d top-down shooter, you need 360 degree smoothess for precision ai$$anonymous$$g.
...the controller is unresponsive in those position, the object rotates smoothly, only when i put the stick in those positions its snaps for a little bit,
Thank you $$anonymous$$auppasarvi so much for this! Solved a problem I've been scratching at for days in 2 simple lines. Greatly appreciate this response!
Your answer
Follow this Question
Related Questions
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
need the game to detect if the analog stick is at the opposite angle 2 Answers
I'm attempting to make a character face the joystick in a top down unity2d game. 1 Answer
Joystick 2D Sprite Rotation and Movement 0 Answers