- Home /
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) {
weight += Time.deltaTime * adjustSpeed;
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.
I'm unclear about your desired behavior or what is going on, but your code is non-standard for aligning an object with the slope. Typically line 20 would be:
toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
Answer by SonicFan · Aug 13, 2014 at 06:42 AM
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) {
weight += Time.deltaTime * adjustSpeed;
transform.rotation = Quaternion.Slerp(fromRotation, toRotation, weight);
}
}
}
Thanks to robertbu for your comment because that was the problem.
Your answer
Follow this Question
Related Questions
Bank Camera Based On Rotation Delta 1 Answer
Rotate smoothly an object when key is up 0 Answers
Smooth reset rotation problem 1 Answer