- Home /
Rotating on a plane
I have a plane that I want to adjust my gameobject to while keeping the ability to rotate around the (new) local Y-axis. Therefore I calculated the planes normal (curUp) and planned on rotating the transform.up towards it.
Quaternion rot = Quaternion.FromToRotation(transform.up, curUp);
transform.Rotate(rot.eulerAngles.x, 0, rot.eulerAngles.z, Space.World);
That does not work as intended obviously, since I'm setting the global y axis to 0. However every attempt to transform the rotation into local space and setting the local y rotation to 0 results in a mess. What's the correct way of doing this in Unity?
Answer by robertbu · Mar 03, 2014 at 12:57 AM
Assuming you have a specific angle you are modifying to rotate your game object, try doing it this way:
transform.up = curUp;
transform.rotation = Quaternion.AngleAxis(angle, transform.up) * transform.rotation;
Hey, thanks for your answer! For some reason I thought transform.up was read only. Good to know! However this method also applies a rotation around the local y axis in some cases.
Have never sen the 'local y' issue, but I don't use this line very often. Here is an alternate for that line:
transform.rotation = Quaternion.FromToRotation(transform.up, curUp) * transform.rotation;
Thank you robertbu, works like a charm! So what was the problem on my own countless attempts? I used transform.rotation = transform.rotation * Quaternion.FromToRotation(transform.up, curUp);
ins$$anonymous$$d of transform.rotation = Quaternion.FromToRotation(transform.up, curUp) * transform.rotation;
I probably could have avoided that with a little insight on Quaternions.
Answer by PlasticGlasses · Feb 03, 2021 at 05:07 PM
Simple code to make, transform and rotate a plane
GameObject thisPlane = GameObject.CreatePrimitive(PrimitiveType.Plane); //create plane
thisPlane.transform.position = new Vector3(1, 1, 1); //set position
thisPlane.transform.Rotate(-90, 0, 0); //set rotation
Your answer
Follow this Question
Related Questions
Code to rotate around a local axis until local Y is 0? 0 Answers
Tank urret rotation - Need help. 1 Answer
2D animation : problem with rotation interpolation 1 Answer
Finding the Euler angles of an object's rotation in terms of a different set of axes 0 Answers
Edited mouselook script rotation clamp not working 0 Answers