x


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!

more ▼

asked Sep 20 '10 at 08:11 PM

ThumbStorm gravatar image

ThumbStorm
420 27 33 42

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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))
}
more ▼

answered Sep 20 '10 at 11:02 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2168
x955
x117

asked: Sep 20 '10 at 08:11 PM

Seen: 1965 times

Last Updated: Sep 20 '10 at 08:11 PM