- Home /
Issues rotating around transform.up
I am trying to set my object to match the rotaion of a joystick. The object needs to rotate around it's transform.up, since it will be at many weird angles. However this code doesn't work.
transform.localRotation = Quaternion.Euler(transform.up * Mathf.Atan2(Input.GetAxisRaw("VerticalRight"),Input.GetAxisRaw("HorizontalRight")) * Mathf.Rad2Deg);
It works when the player is at any rotation like (0,y,0), however if we move to (45,y,32) all of a sudden the object rotates to weird angles. What am I doing wrong, or is there a better way to control this rotation?
Have you tried using Transform.RotateAround or Quaternion.AngleAxis?
Tried messing with AngleAxis thanks to robertbu, however it didn't work.
After testing it, it seems even after I've rotated the transform to say (0,0,90), it will reset to (0,y,0) as if it's rotating around Vector3.Up rather then transform around what would now be Vector3.Right
Answer by robertbu · Feb 21, 2014 at 04:03 PM
Try this:
transform.localRotation = Quaternion.AngleAxis(Mathf.Atan2(Input.GetAxisRaw("VerticalRight"),Input.GetAxisRaw("HorizontalRight")) * Mathf.Rad2Deg, transform.up);
unfortunately that didn't seem to work, I'll try and make a web build when I get home and link it here to show the issue.
Edit:
After testing it, it seems even after I've rotated the transform to say (0,0,90), it will reset to (0,y,0) as if it's rotating around Vector3.Up rather then transform around what would now be Vector3.Right
Answer by livevlad · Feb 24, 2014 at 09:38 AM
Try this:
transform.rotation = Quaternion.Euler(transform.up * Mathf.Atan(Input.GetAxisRaw("VerticalRight"),Input.GetAxisRaw("HorizontalRight")) * Mathf.Rad2Deg);
I think the problem comes from the localRotation because your transform.up return the up direction in world context.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Character Not Rotating 0 Answers
Rotate object like a safe dial 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer