0
\$\begingroup\$

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);
        }


    }
\$\endgroup\$
  • 1
    \$\begingroup\$ Don't you want Quaternion.LookRotation(moveVector, Vector3.up);? \$\endgroup\$ – Alex F Mar 24 at 4:15
  • \$\begingroup\$ 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... \$\endgroup\$ – DMGregory Mar 24 at 12:03
  • \$\begingroup\$ Thanks alot for all the help guys. \$\endgroup\$ – Todd Weiss Mar 24 at 21:31
0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 24 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.