- Home /
Standard FPS Controller - Smoothing Movement
I noticed that when you move the Standard Assets FPS controller via the WSAD keys, the moment you let go of any key to stop it from moving it instantly halts any movement it was making.
I tried looking through the FirstPersonController.cs
script to find anything to do with smooth the transition between movement and suddenly stopping, but I couldn't seem to find any. I saw the smoothing option for the camera movement but nothing to do with the physical movement.
Admittedly programming isn't my strong suit. I would very much appreciate if anybody has a solution to smooth the sudden halting.
Answer by SolomonG · Aug 25, 2017 at 11:07 PM
A simple solution would be to employ trapezoidal acceleration. It sounds scary but it's not. Essentially you simply speed up or deaccelerate according to a maximum acceleration (change in velocity/speed), as opposed to always setting you actual speed to how fast you want to go.
In pseudocode form:
update() {
// If the difference is more than how much we can accel in a frame
if (actualSpeed < targetSpeed - maxAcceleration) actualSpeed += maxAcceleration;
// If the difference is more than how much we can deaccel in a frame
else if (actualSpeed > targetSpeed + maxDeacceleration) actualSpeed -= maxDeacceleration;
// else we're either on target or close enough (+/- maxAccel/maxDeaccel)
else actualSpeed = targetSpeed
}
I will try copy-pasting your solution into the Update section of the code. I have a lot to learn so I'll have to take your word for it, haha. :)
Okay so I copied the code you wrote beneath the private void start section but above the private void update section. It looks like this so far:
void update()
{
// If the difference is more than how much we can accel in a frame
if (actualSpeed < targetSpeed - maxAcceleration) actualSpeed += maxAcceleration;
// If the difference is more than how much we can deaccel in a frame
else if (actualSpeed > targetSpeed + maxDeacceleration) actualSpeed -= maxDeacceleration;
// else we're either on target or close enough (+/- maxAccel/maxDeaccel)
else actualSpeed = targetSpeed
}
I saved the script but I received an error:
Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/FirstPersonController.cs(70,7): error CS1002: ; expected
I have no clue what this means or how to fix it. I've missed something haven't I?
I know this is very late, but you've missed a semicolon at the end of the last "else" statement :)
Answer by lastnoob765 · May 08, 2021 at 09:20 AM
Change the input from GetAxisRaw to GetAxis and use lerp to smooth it out.So when i let go of the keys it will have some momentum left and stop 0.1-1s later.Depending on lerp amount