# Keeping rigidbody upright in relation to slope

Hello, I want to make a snowboarding game and I want the character to always be perpendicular to the slope.

So far I’ve tried freezing the rotation of the x and z axis but that just keeps the character completely straight. I want him to follow the contours without having to increase the mass just to get him to stay put. If anyone could help that’d be awesome

The most straightforward way of doing this would be to keep track of the current ‘up’ direction, and continually raycast downwards to find the normal of the surface below and update accordingly. Some sample code (how I would do it)-

``````// First off, make sure that you are freezing the rotation
// of the rigidbody since we are doing that manually now.

// This determines how fast the rigidbody rotates towards the correct value
public float dampingRate;
// This determines how far it can detect the ground from
public float rayLength;
// Set this up with the correct 'ground' layers

Vector3 currentUp = Vector3.up;
void FixedUpdate()
{
RaycastHit hit;
if(Physics.Raycast(transform.position, -currentUp, out hit, rayLength, mask))
{
currentUp = hit.normal;
}
Quaternion targetRotation = transform.rotation;
if(rigidbody.velocity.magnitude > 0)
{
targetRotation = Quaternion.LookRotation(rigidbody.velocity, currentUp);
}
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, targetRotation, Time.deltaTime * dampingRate);
}
``````

This will always rotate the object towards its velocity- as calculated by physics. Assuming that it slides down hills in a normal fashion, this should result in nicely directed rotations.

i cant make him stay straight in the first place, he was, i saved, came back and now he leans toward the camera