# Quaternion.LookRotation with Joystick

I am having an issue i cannot seem to figure out within my code. What this code does at the moment is that it rotates the 3D Model around the z axis but i need it to rotate around the y axis. Any help would be greatly appreciated.

void Update ()
{
Vector3 moveVector = (Vector3.up * joystick.Horizontal + Vector3.left * joystick.Vertical);
if (joystick.Horizontal != 0 || joystick.Vertical != 0)
{
transform.rotation = Quaternion.LookRotation(Vector3.forward, moveVector);
}

}

• Don't you want Quaternion.LookRotation(moveVector, Vector3.up);? – Alex F Mar 24 '19 at 4:15
• It looks that way @AlexF, so I think that's on the right track to an answer. The one problematic detail is that moveVector can be parallel to the vertical axis at the moment. I'm not sure if they meant to multiply joystick.Horizontal by Vector3.forward instead of Vector3.up... – DMGregory Mar 24 '19 at 12:03
• Thanks alot for all the help guys. – Todd Weiss Mar 24 '19 at 21:31

you could also use:

float speed = 2f;
void Update ()
{
Vector3 moveVector = (Vector3.up * joystick.Horizontal + Vector3.left *
joystick.Vertical);
if (joystick.Horizontal != 0 || joystick.Vertical != 0)
{
transform.eulerAngles
+= Vector3.up * Mathf.Atan(joystick.Horizontal) * Mathf.Rad2Deg * speed *
Time.deltaTime;
transform.eulerAngles
+= Vector3.right * Mathf.Atan(joystick.Vertical) * Mathf.Rad2Deg * speed *
Time.deltaTime;
}
}


you should also use Time.deltaTime for things like this to decouple the simulationspeed from the frametimes, i have included that in the example above.

• Modifying Euler angles component-by-component often leads to different results than you anticipate. Doubly so if you do it in two steps like this, where the engine will convert them to a quaternion in-between, then hand you a potentially very different set of Euler angles for the second step. This will also continue to spin the object proportionate to the joystick deflection, rather than turning the object to face the direction the stick is pointing and stopping there as OP seems to want by using LookRotation. Overall, I would not recommend using the approach shown in this answer. – DMGregory Mar 24 '19 at 12:23
• @DMGregory Component-by-Component works fine if all components are made into quaternions first. And you remember to reverse the order. – Weckar E. Aug 15 '20 at 21:29