- Home /

**Question**by SudoSandwich · Apr 14, 2015 at 10:51 PM · rotationphysicseulerangleslocalrotationlocaleulerangles

# Bike stay upright based on surface normal

I'm having issues thinking of a good way to align my bike to the surface its riding on by modify the local z axis, without messing up the rigidbody simulation. I am able to get my bike to balance by modifying the transforms local euler angles as such:

```
var euler = transform.localEulerAngles;
// Only works for staying upright relative to ground plane
transform.localEulerAngles = new Vector3(euler.x, euler.y, -steerLean);
```

The issue here is that when I go up a vert ramp the bike aligns, obviously, straight upwards. I cannot think of / recall a good way to keep the object upright but keep the rigidbody simulation intact otherwise.

I have tried to use Quaternion.FromtoRotation but this does not seem to want to play nicely. The object flops from left to right and eventually just falls over.

My bike uses WheelColliders and is working fine other than this issue.

Picture above for reference. The bike is moving and should be leaning to the right.

**Answer** by Spinnernicholas
·
Apr 14, 2015 at 11:14 PM

You can get the normal with WheelCollider.GetGroundHit.

You pass it a WheelHit and it fills it up with the current physics information, including the normal.

If both wheels are on the ground, to get the actually vector you want, you will have to average the two normal vectors. Otherwise, just use the normal for the wheel that is touch the ground.(WheelCollider.GetGroundHit return false if the wheel is not touching the ground.)

Or...

Add mass for the bike and a possible rider and place at their respective centers of mass. If tuned correctly, gravity will pull the the bike to the right angle, balanced by the bikes force against the surface.

Set the local up angle of the bike to the above calculated normal.

So, I understand the part about getting the normal of the two tires. The part I am struggling with is how to convert that vector to the local euler angle I need for the z axis of my bike.

I tried the mass idea and it is something I can work with. I applied force relative to the inverse of the average ground normal of the two tires and lowered the center of mass. Now to turn I am adding relative torque instead of directly modifying the euler angles.

Thanks for the help

Ok, cool, good, because I'm still struggling to work out the details of how to do the first one. Involves fun vector math.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.