Before updating to Windows 10 you may need to return your Pro license. More information here

Smooth align Object with normal surface of ground

Dear all,

i just align a ridigbody (rotation freezed) with the ground. All works well with this code. I send ray to the ground and rotate the object with normal vector given by the hit on raycasting.

void FixedUpdate() { RaycastHit hit;

 Vector3 theRay = transform.TransformDirection(Vector3.down); // Convert from local space to world space

 Debug.DrawRay(transform.position, theRay, Color.red);

 if (Physics.Raycast(transform.position, theRay, out hit)) 
     transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);


However, the object doesn't align smoothly with the ground: rotation are rough. How i can do that?

Thanks for support,


more ▼

asked Mar 19, 2011 at 02:29 PM

avatar image

1 3 2 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

What is it supposed to do? Align with the ground directly under you? In that case, the problem is that your first line aims a ray at whatever angle your bottom faces. After the code tilts you that way, you are now aiming at a different section of the ground, so gets retilted next frame, and so on.

Try using just theRay = Vector3.down; Also a little odd that the ray is not a ray.

I also prefer just looking the normal up directly, on the assumption that a raycast is expensive, and does this lookup anyway:

// find normal under spot (C#):
TerrainData td = Terrain.activeTerrain.terrainData;
Vector3 grndNorm = td.GetInterpolatedNormal(spot.x/td.size.x, spot.z/td.size.z);
more ▼

answered Mar 19, 2011 at 08:34 PM

avatar image

Owen Reynolds
27.9k 5 39 105

Hi owen, Thanks for you reply. I have a motocycle system with 2 wheels colliders. The motocycle jump and land on the terrain. I would like to align the motocycle with the ground on landing because without it's pretty unstable. Besides the motocycle (BMX) spin in the air (360, 720).

i tried your 2 ideas. However i still have rough rotation when apply the rotation from the normals i got. Thank you for your support, Dragon

Mar 28, 2011 at 05:20 PM Dragon

@Owen i tried this with the terrain:

TerrainData td = Terrain.activeTerrain.terrainData; Vector3 grndNorm = td.GetInterpolatedNormal(transform.position.x / td.size.x, transform.position.z / td.size.z); transform.rotation = Quaternion.FromToRotation(grndNorm);

Mar 28, 2011 at 05:21 PM Dragon

For a motorcycle, I'm thinking you DON'T want to align on the normals. Imagine riding crosswise on slightly sloping terrain, making a small jump, even. The bike should be straight up, and land straight up. Put another way, bike don't rock L/R over rough ground. All the slope does (which is the Normal) is determine whether you skid sideways. Seems like when you land, the bike should gradually tilt "up," or whichever way you are tilting to steer.

Mar 28, 2011 at 08:30 PM Owen Reynolds

@Owen Thanks for your reply, i just wanted to align with ground just before landing (that's the thing a fail to do). When bike system is grounded i only use a configurable joint with axis Z locked (this allow me to ride crosswise properly with downhill). In reality my bike system is to simulate a skier. I have one wheel between nose of skis, and another one between the tails of the skis. Wheels will be trasparent. Thanks Again ! Thomas

Mar 29, 2011 at 11:23 AM Dragon
(comments are locked)
10|3000 characters needed characters left

Instead of: transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); Try: transform.rotation = Quaternion.Slerp(transform.up, hit.normal, time); where the time variable is usually: time = Time.deltaTime * interpolateSpeed;

more ▼

answered May 23, 2012 at 04:49 PM

avatar image


(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 19, 2011 at 02:29 PM

Seen: 6352 times

Last Updated: May 23, 2012 at 04:49 PM