Question by
tommyzat · Apr 28, 2016 at 09:30 AM ·
movementmovement scriptaccelerationmovementsaccelerate
Movement acceleration
Hello, I'm trying to make a movement engine that goes like this: On W press, start with a slow speed then gradually go up as long as you keep holding W. After W is no longer pressed, quickly deaccelerate and then stop.
This is a part of my script. What's wrong with it?
void Update()
{
if(isgrounded)
{
movedirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
movedirection = transform.TransformDirection(movedirection);
speed = Mathf.Lerp(initialspeed, newspeed, timespeed * Time.deltaTime);
if (Input.GetKey(KeyCode.W))
{
newspeed = 30;
}
if (Input.GetKeyUp(KeyCode.W))
{
newspeed = 5;
}
movedirection *= speed;
}
Comment
Answer by tommyzat · Apr 29, 2016 at 09:02 AM
Or is therea another way instead of using mathf.lerp for accelerating?