- Home /
Cant understand why object rotates when setting transform.up to normal
Hello!
I am using this script to find normal on mesh under cursor:
http://unity3d.com/support/documentation/ScriptReference/RaycastHit-barycentricCoordinate.html
so it gives me interpolatedNormal
and than I use following code to align my object( cube ) with surface's normal
gameObj.transform.up = interpolatedNormal;
Problem with this is that object rotates around y-axis while I move cursor over mesh and i just want him to be aligned with normal but preserve its rotation around y-axis. How can I achieve this? Any advice appreciated
Answer by Owen-Reynolds · Jul 01, 2011 at 05:42 PM
As you've noticed, setting "up" to a direction doesn't say which "spin" you should have (known as a degree of freedom.) The lookAt command has an optional 2nd input to account for this -- if you don't add it, the computer assumes you want that free spin to be as "up" as possible. Using transform.up=
doesn't seem to have a good way to specify that free spin, so the computer accidentally bounces it around on you. You could try aiming it first, to trick "up" into giving a consistant free-spin:
[untested]
transform.LookAt(whereever it should be aimed);
transform.up = theNormal;
I don't like to fight with setting "up", so use this:
// facing is the 0-360 "free" y-spin I want:
transform.rotation = Quaternion.Euler(0,facing,0);
// We are on flat ground, and spun. Now tilt with ground:
// compute tilt rotation:
Quaternion tilt = Quaternion.FromToRotation(Vector3.up, norm);
// apply tilt rotation:
transform.rotation = tilt*transform.rotation;
Thank you for your solution but i receive same results - rotation around y-axis. I try to study quaternions so I cant understand what is happening.
O$$anonymous$$. So problem solved by putting $$anonymous$$us before norm.
Quaternion tilt = Quaternion.FromToRotation(Vector3.up, -norm);
So there is no any rotation now around y-axis happening. But i dont understand why I should put this $$anonymous$$us there before norm.
I have to draw a picture. V3.up is straight up, and norm (the way the ground points) should be mostly up. FromToRot(up, norm)
computes the amount and angle of tilt between them. If your normals are flipped so they face down, clearly -norm is the solution. Otherwise -- I know the math but I still sometimes change random +/-'s and flip multiplies until it works, and then figure out why.
Answer by Waz · Jul 01, 2011 at 12:00 PM
Maybe what you want to do is:
gameObj.transform.rotation = Quaternion.LookRotation(interpolatedNormal);
this will make him look out from the normal, but with his head towards the sky. Some variation of that sounds like what you want, but you may need to further modify it.
Thanks for reply it tried it but got same result - cube that is moving along surface is rotating but only difference that this time around z-axis since Quaternion.LookRotation makes "forward" look along vector that is passed to function. So now z-axis is pointing out from surface and cube rotates around it.