# Aligning object to surface normal with Quaternion.LookRotation

I have a cube and a terrain object. I’m trying to cause the cube to orient itself properly with the terrain. For now I have my cube floating above the terrain and I’m casting a ray downwards onto the terrain and trying to set the orientation of the rigid body based on the RaycastHit normal. As I move the game object over the terrain the object seems to be rotating around the z axis properly, but I can understand why the X axis isn’t effected.

Here is the code I have attached to the cube:

``````function update() {

var hit1: RaycastHit;

if (Physics.Raycast(transform.position,-Vector3.up,hit1)) {
var objectForward : Vector3 = transform.TransformDirection(Vector3.forward);
transform.rotation = Quaternion.LookRotation (objectForward, hit1.normal);
//transform.position = hit1.point;
}
}
``````

And here is the code with the debugging… if it helps.

``````function update() {
var hit1: RaycastHit;

Debug.DrawRay(transform.position,-Vector3.up * 100,Color.red);

if (Physics.Raycast(transform.position,-Vector3.up,hit1)) {
Debug.DrawRay(hit1.point,hit1.normal * 100,Color.blue);
var objectForward : Vector3 = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, objectForward * 100,Color.yellow);
Debug.DrawRay(hit1.point,hit1.normal * 100,Color.green);
transform.rotation = Quaternion.LookRotation (objectForward, hit1.normal);
//transform.position = hit1.point;
}
}
``````

Quaternion.LookRotation aligns the object’s Z direction exactly to the forward (the first parameter) and then rotates it around that vector axis until it its Y direction is aligned as closely as possible with the supplied upward direction (the second parameter). In other words, of the two vectors, the first gets priority when they can’t both be aligned exactly. You probably want to look at Quaternion.FromToRotation to align the upward direction to a surface normal. The manual page for the function gives an example of how to do this.

2 Likes

Thank you so much. I am having luck with FromToRotation by setting the rotation to Quaternion.FromToRotation(Vector3.up,hit.normal). The script reference doesn’t really explain whats going one, but I assume this is just taking the Objects up orientation and rotating to match the normal of the surface?

I’m encountering another problem now though - The cube maintains a constant y axis orientation… or for a lack of a better word it’s always pointing straight forward. Ideally I’d like to set the y rotation and then have it align to the surface normal in both x,z rotations but maintain the y axis which will be controlled by user input. I was hoping it would be as simple as setting the object to align to the surface and then just rotating it around it’s own y axis. When I do that however I can see that the local transform up vector is wobbling off of the hit.normal vector.

I can’t quite wrap my head around why that would be occurring, but I know this is a fairly common thing to need to do. I have been digging through other forum topics and I see other people mentioning use of LookRotation method in this situtation, but I that just brings me back to my original post.

Found an answer to this question here: