Updating game objects rotation after joystick rotate

Hi, I have managed to find this problem on other threads but have not as yet found the solution and I think it is something to do with needing to use math functions on the game object’s rotation?

Take Ethan as the game object and he is facing you.

My virtual joystick rotates him back and forth when I use the up/down part of the joystick…all good!

The virtual joystick rotates him left and right when I use the left/right part of the joystick…all good!

But…

If I rotate Ethan so he is facing left and the use the up or down joystick control he tilts left and right not back and forth.

I know this is to do with his transform rotation needing to be updated to reflect the new angle (angle I think?) but don’t know how.

Currently I have this that is not doing what is required:

transform.Rotate( Vertical, Horizontal, 0f );

Hope I have explained right, please ask if you need more info.

Thanks

2 Answers

2

You are tilting his rotation in terms of World Space. When you want to tilt him according to his own Local Rotation, you have to multiply the rotation you wish to apply by his Local Rotation:

transform.rotation *= Quaternion.Euler(new Vector3(x, y, z));

Note: Don’t do this when turning him only when tilting him.

Thanks, kinda not sure I follow? I tried this earlier and felt so close: transform.Rotate (Vertical, Horizontal, 0, Space.World); but it still goes bonkers. So do you mean I do this: transform.Rotate (Vertical, Horizontal, 0, Space.World); then if the up/down pressed follow with this: transform.rotation *= Quaternion.Euler(new Vector3(Horizontal, Vertical, transform.rotation.z)); ?

I see what the problem is, It is because you are rotating the object with the axis from the joystick.

I think your joystick gives axis (minus 1 till 1, in x and y directions).

So you can just simply set the rotation RotateToWards this direction:

//for example
Vector3 rot = new Vector3(Input.GetAxis("Vertical"), 0 , Input.GetAxis("Horizontal"));

transorm.rotation = Quaternion.LookRotation(rot);

Hope this works a bit

Hi, thanks, see my code snippet below that appears to work using transform.Rotate. Will give your suggestion a go to see what difference there is