Rublizt
February 20, 2017, 9:40am
1
I have a simple 2D object that I want to slowly decelerate before it stops.
This is the script that is attached to the object (it’s just the basic movement):
void Update () {
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (horizontal, vertical, 0.0f);
transform.position += move * speed * Time.deltaTime;
}
Hi,
you need to detect if your Vector3 is zero (if the player has released) the input and interpolate to zero then.
Something like this :
private Vector2 _input;
private Vector3 _move;
void Update ()
{
_input = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
if (_input != Vector2.zero)
{
_move = (Vector3) _input;
}
else
{
_move = Vector3.Lerp (_move, Vector3.zero, decelerationSpeed * Time.deltaTime);
}
transform.Translate (_move * speed * Time.deltaTime);
}