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\$
3
  • 1
    \$\begingroup\$ Don't you want Quaternion.LookRotation(moveVector, Vector3.up);? \$\endgroup\$ Commented Mar 24, 2019 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\$ Commented Mar 24, 2019 at 12:03
  • \$\begingroup\$ Thanks alot for all the help guys. \$\endgroup\$ Commented Mar 24, 2019 at 21:31

2 Answers 2

2
\$\begingroup\$

It looks like the comments from three years ago solved OP's problem, so I'm posting them as an answer so we can keep this question from continuing to be bumped.

It looks like you want to do this:

void Update () { // Construct a direction in the horizontal plane. Vector3 moveVector = Vector3.forward * joystick.Horizontal + Vector3.left * joystick.Vertical; if (moveVector != default) { // Rotate around the y axis, // to point our local z+ axis in that direction. transform.rotation = Quaternion.LookRotation(moveVector); } } 
\$\endgroup\$
-1
\$\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\$
2
  • 2
    \$\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\$ Commented Mar 24, 2019 at 12:23
  • \$\begingroup\$ @DMGregory Component-by-Component works fine if all components are made into quaternions first. And you remember to reverse the order. \$\endgroup\$ Commented Aug 15, 2020 at 21:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.