- Home /
How would I make an object rotate where the joystick is facing.
Hello, I am making a mobile game about a 3D plat former where the player is only able to rotate on its z-axis. I've been having a lot of trouble lately trying to find online how to make it possible to make the player rotate based on the joystick's direction. Could anyone please help me with this? I really have no clue on how to start this. Thank you.
Answer by nateonguitar · Jan 08, 2019 at 02:56 PM
I'm on my phone so this is a little harder, but have you looked into using the arc tangent function? I'm assuming it's built into Mathf.
Basically you give it your inpit's Mathf.Atan(y/x) and arctan will give you the angle your joystick is facing.
Edit: found the docs and used it in the previous sentence:
https://docs.unity3d.com/ScriptReference/Mathf.Atan.html
Here's one gotcha though, arctan(y/x) and arctan(-y/-x) are equal so you'll need to check if x < 0 always to make sure what quadrant you're in.
Also check out this video for setting up the joystick, you can see in his script late in the video how he gets x and z values, I think the get axis functions return a number between -1 and 1
https://youtu.be/384_g0f7K_I