- Home /
Lerp movement speed
Hello, all! I have a ship that moves up, left, right, and down using this script:
var moveSpeed : float = 5.0;
function Update (){
var x = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(x, y, 0, Space.World);
}
Simple stuff, right? Well I was wondering if there was a way for me to lerp the movement speed on each key press. For instance, when I hit the up key (When GetAxis"Vertical" == 1), the movement speed starts off slow, and builds up over a set float variable (number of seconds) to the target moveSpeed, and when the key is released, the same thing occurs, except backwards.
This would create a very nie 'sliding' kind of effect that I think would be appropriate for a space scene.
Thanks! - YA
Answer by markpdolby · Nov 12, 2012 at 05:55 PM
var maxSpeed = 10;
var x = 0;
function Update(){
if(Input.GetAxis("Horizontal") == 1)
{
x = Mathf.Lerp(x, maxSpeed, Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed);
}
else
{
x = Mathf.Lerp(x, 0, Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed);
}
//Then the same for Y etc.
}
Something along those lines should point you in the right direction. Hope that helps.
That looks pretty good. I'll get back to you on how it works out.
No... There's definitely something missing from here
I mean, besides the obvious "moveSpeed" variable and the actual Translation call.
Well, I hit the key, and the ship moves... I can't quite tell if it's lerping or not (I assume it must be).
What's missing is a Dampen variable. I'd like to have direct control over how much time it takes to accelerate/decelerate.
Also, the ship moves EXTRE$$anonymous$$ELY quickly
How is this working with the keys? I didn't know Input.GetAxis() could return anything but 1/0!
Also, I would like to know how I would get this to function for moving the ship Downwards or left.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083744im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to lerp over time help? 1 Answer
Wait a fraction of a second within a Function Update() 1 Answer
rotate object by list values 0 Answers
My code break character controller 2 Answers
Changing Color Based on Z position 2 Answers