# Smooth tilting rotation on slopes like cars?

Hello,i know that there is many other questions like this one but non of them is what i want.

I’m making a game that i wan’t when my player starts running to smoothly tilt on the slope and searched Google but i didn’t find what i want but after so much searching yesterday I found this code.

``````   var adjustSpeed : float = 1;
public var fromRotation : Quaternion;
private var toRotation : Quaternion;
private var targetNormal : Vector3;
private var hit : RaycastHit;
public var weight : float = 9;

function Start() {
targetNormal = transform.up;
}

function Update() {

Debug.DrawRay(rotater.transform.position,-rotater.transform.up* 2, Color.yellow);

if(Physics.Raycast(rotater.transform.position, -rotater.transform.up, hit)) {

fromRotation = transform.rotation;
toRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
weight = 0;

if(weight <= 1) {
transform.rotation = Quaternion.Slerp(fromRotation, toRotation, weight);
//Or to smooth the weight
//tranform.rotation = Quaternion.Slerp(fromRotation, toRotation,
//                                        Mathf.SmoothStep(weight));
}
}
}
``````

This code is working Fine but it’s rotating all of the axis but i don’t want this code to rotate the y axis or my local y axis.

So i would like to know if there is a better code than this or i can modify this code to work better.

Thanks.

Sorry,i copied the wrong script because i was doing some tests on the script.

But i finally Fixed it and here is the script.

``````var adjustSpeed : float = 1;
public var fromRotation : Quaternion;
private var toRotation : Quaternion;
private var targetNormal : Vector3;
private var hit : RaycastHit;
public var weight : float = 9;

function Start() {
targetNormal = transform.up;
}
function Update() {
if(Physics.Raycast(transform.position, -Vector3.up, hit)) {

targetNormal = hit.normal;
fromRotation = transform.rotation;
toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
weight = 0;

if(weight <= 1) {