- Home /
Slerp not working
Hello,
In the script below once the left/right buttons are pressed a motorcycle leans and then when the button is released it leans back to center. It works perfectly on the first couple of button presses but after that it jumps to the next transform instead of slowly moving.
var toLeft : Transform;//bike lean left//
var toRight : Transform;//bike lean right//
var toCenter : Transform;//bike lean center//
var speed = 0.01;//speed of lean//
var returnCenter : boolean = false;//tells script it's ok to center bike//
function Update () {
if (Input.GetKey("left")) { returnCenter = false; transform.rotation = Quaternion.Slerp (transform.rotation, toLeft.rotation, Time.time *speed);
} if (Input.GetKeyUp ("left")) { returnCenter = true;
} if (Input.GetKey ("right")) { returnCenter = false; transform.rotation = Quaternion.Slerp (transform.rotation, toRight.rotation, Time.time *speed);
} if (Input.GetKeyUp ("right")) { returnCenter = true;
} if (returnCenter) { transform.rotation = Quaternion.Slerp (transform.rotation, toCenter.rotation, Time.time * speed); }
}
Any ideas would be great!
Answer by skovacs1 · Sep 20, 2010 at 11:02 PM
Using Time.time will give you the time since the start of the game, which is why you are jumping after a bit.
Replace Time.time with Time.deltaTime.
This is almost correct, but still wrong. If you do this, you will always be going at some small amount * speed along the rotation from your current rotation to your target, meaning that with a large enough speed, it will quickly start any motion and then slow as it approaches the target without ever reaching it.
You need a control variable to know how far you are leaned and you should rotate from one orientation to another, not your current orientation because that will change. Try:
var lean : float; //amount leaning var moving : boolean; //do we do anything this frame? var toLeft : Transform;//bike lean left// var toRight : Transform;//bike lean right// var speed = 0.01;//speed of lean//
function Update () { moving = false; if (!Input.GetKey("left") && !Input.GetKey("right") { else if(lean < 0.5) { moving = true; lean += Time.deltaTime speed; if (lean > 0.5) lean = 0.5; } else if(lean > 0.5) { moving = true; lean -= Time.deltaTime speed; if (lean < 0.5) lean = 0.5; } } else if (Input.GetKey("left")) { moving = true; lean -= Time.deltaTime speed; } else { moving = true; lean += Time.deltaTime speed; } transform.rotation = Quaternion.Slerp (toRight.rotation, toLeft.rotation, lean) }
If you want it to smooth at the ends, you could try Mathf.SmoothStep on lean. If you want it to smooth at the ends and at the start of your rotation, you would need to treat the motions separately and store your start rotation. Something like this should do it, handling when you have simultaneous key press/release events.
var lean : float; //amount leaning var toLeft : Transform;//bike lean left// var toRight : Transform;//bike lean right// var toCenter : Transform;//bike lean center// var origin : Quaternion;//where to lean from// var destination: Quaternion; //where to lean to; var speed = 0.01;//speed of lean//
function Update () { if(Input.GetKeyDown("left") && Input.GetKeyDown("right")) { origin = transform.rotation; destination = toCenter.rotation; lean = 0; } else if(Input.GetKeyDown("left") { origin = transform.rotation; destination = toLeft.rotation; lean = 0; } else if(Input.GetKeyDown("right") { origin = transform.rotation; destination = toRight.rotation; lean = 0; } if(Input.GetKeyUp("left") || Input.GetKeyUp("right") { if(Input.GetKey("left") { origin = transform.rotation; destination = toLeft.rotation; lean = 0; } else if(Input.GetKey("right") { origin = transform.rotation; destination = toRight.rotation; lean = 0; } else { origin = transform.rotation; destination = toCenter.rotation; lean = 0; } } lean += Time.deltaTime * speed; transform.rotation = Quaternion.Slerp (origin, destination, Mathf.SmoothStep(lean)) }
Your answer
Follow this Question
Related Questions
Smoothly flipping an object on one axis on click then back again 1 Answer
Scale gameObject 1 Answer
How to prevent Z axis rotation ? 1 Answer
Limit rotation angle on Z axis while slerp 1 Answer
Rotate overtime to match waypoint 1 Answer