- Home /
Rotate floor plane in-game via C# script
Hi,
I have a floor plane which is initialised before the game starts.
I have a script which later generates a vector. I want this vector to be the new normal for my floor plane.
I want to be able to rotate the floor plane such that it's normal is this above vector, (i.e. the plane becomes perpendicular to this vector).
I have looked into rotations and quaternions but it all seems very mathematically complicated.
The floor's initial rotation is (0, 0, 0) in the transform. Is there a simple way to simply change it's angle in the 3D space? Thanks.
Answer by joelv · Mar 31, 2015 at 11:38 AM
One way to do it is to use the function Quaternion.FromToRotation http://docs.unity3d.com/ScriptReference/Quaternion.FromToRotation.html
You should be able to call this like:
transform.rotation = Quaternion.FromToRotation(transform.up, myNewNormal) * transform.rotation;
Which generates a quaternion that rotates your previous up vector to your new up vector (normal) and then applies that to the current rotation.