I want my character to slightly slide like it is on ice (but only a little bit).
I feel like what I have should work but it seems when I try to stop the character slowly it just doesn’t and stops straight away. I’ve debugged and the script does recognise when the character stops moving and the body.velocity in the if(is_stopping) statement is getting the correct horizontal/vertical input but doesn’t apply the velocity for some reason.
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
if(horizontal != 0 || vertical != 0){ //If there is movement keys pressed
moving = true;
}
else if (moving){ //If there is NO movement keys pressed and moving is true
moving = false;
is_stopping = true; //Set stopping to true to begin the slow stopping
previous_horizontal_traj = previous_horizontal; //Record current player direction
previous_vertical_traj = previous_vertical;
}
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
//Move the player normally
if(moving){
body.velocity = new Vector2(horizontal * move_speed, vertical * move_speed);
}
//Stop the player slowly
if(is_stopping){
if(stop_speed <= 0.0f){
is_stopping = false; //character has stopped moving
stop_speed = 20.0f; //Reset stop speed
}
body.velocity = new Vector2(previous_horizontal_traj * stop_speed, previous_vertical_traj * stop_speed);
stop_speed = stop_speed - slow_stop_rate;
}
previous_horizontal = horizontal;
previous_vertical = vertical;
}
Not sure if this is the best way.
Any help is super appreciated!