I’m moving my player by setting their x velocity to Input.GetAxisRaw(“Horizontal”), and it works fine, but when I try to add a force to my player, it moves for one frame before being set to the Input again. What I would want is for the force to work, but still have some control over it. Say, if I added a force of 10 to the right, and I was holding right, I would move with a force of 11, and 9 if I was holding left. I’ve been trying simulating velocity separately and then setting the actual velocity to that plus Input, but that seems like an overcomplication, and I really don’t want to deal with that.
How to have add forces to rigidbodies while still being able to move them by setting their velocity?
Well… force is adding a value to the velocity, and you immediately overwrite that, of course it can’t work.
What you could do is to change the velocity gradually.
Vector3 targetVelocity = GetTargetVelocityFromInputSomehow();
body.velocity = Vector3.MoveTowards(body.velocity, targetVelocity, controlAcceleration * Time.fixedDeltaTime)
That’s just the start. You can change the acceleration based on whether the key is pressed or none is, whether you are in air or grounded, accelerating/decelerating, etc. to reach your desired gameplay feel.