- Home /
How to lock a transform's local up axis rotation on any surface
I've been trying to make a script that keeps an object's local Y axis from rotating on any surface. This line of code allows this to be possible on MOST surfaces:
transform.up = Vector3.Cross(transform.forward, transform.right);
But the problem with this code is when the object is transitioning from being upside down to not and vice versa, the Y axis starts rotating uncontrollably for a few seconds then settles down. Any suggestion?
Answer by toomasio · Jan 16, 2019 at 06:47 PM
Use raycasting to find the normal of the surface your character is standing on and use: https://docs.unity3d.com/ScriptReference/Quaternion.FromToRotation.html
might work better.
I've tried that, but it yields the same result. I have a player that goes and rides on walls and ceilings, but now I need an object that mimics that player's lookAtPoint transform.up to its.
I've tried:
transform.rotation = Quaternion.FromToRotation(Vector3.up, lookAtPoint.up); transform.up = lookAtPoint.up; and Raycasting the plane that they're on.
But it does the same thing: works fine until it transitions between being upside down to not being upside down.
Sorry, but actually your solution works! Thanks! I was being an idiot.