So I'm building a 2d top-down game and I need to control my character using unity cross-platform standard asset single joystick.
The problem is with the rotation, I don't want to use the dual-stick for some idea related reasons and the designs and the game mechanics that was considered for this game.
So I want the character to have the same rotation as the joystick and since the joystick only has the horizontal and the vertical axies it's hard to get a descent rotation.
So any ideas how to do this? if you need further clarification let me know.


1 Answer 1


In the mathf class there is a function called Atan2, which is exactly what is needed here.
so all that is needed to be done is to use the mobile single stick control in the cross-platform standard assets and get the Horizontal and Vertical axis from that and use the Atan2 to get the radiant and then turn it to degrees and use it to set the z angle of the player.

Player.transform.eulerAngles = new Vector3(0, 0, -(Mathf.Atan2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * Mathf.Rad2Deg));

or for simplicity :

float Haxis = CrossPlatformInputManager.GetAxis("Horizontal");
float Vaxis = CrossPlatformInputManager.GetAxis("Vertical");
float Zangle = Mathf.Atan2(Haxis, Vaxis) * Mathf.Rad2Deg;
Player.transform.eulerAngles = new Vector3(0, 0, -Zangle);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .