#Sliding Rigidbody FPS Character issue

1 messages · Page 1 of 1 (latest)

dim hawk
#

btw i have tried googling about the problem but i couldnt find anything

proper niche
#

AddForce does just that - it adds a force to your Rigidbody.
This usually is not a good thing when you want 'snappy' input, because due to physics, the Rigidbody will keep moving, after the force has stopped being applied.

There are 3 ways around this:

  1. Control the velocity directly. Gives you the most control, but completely prevents use of AddForce. Usually advised against.
  2. Increase drag. This will slow your Rigidbody down quicker, but you will also need to increase applied force to get it to move in the first place. Also can have unintended side-effects on gravity.
  3. Apply counter-force when there is no input. A compromise between the former two; if done right, you can get your Rigidbody to slow down faster, while maintaining gravity and the ability to use AddForce.
dim hawk
#

How do i apply counter-force?

proper niche
#

using AddForce and giving it a vector that points in the opposite direction of the Rigidbody's current velocity (on x and z axes, that is. also applying counter force on y would again mess with gravity)

dim hawk
#

Sorry but do you mind showing a example because im not really sure on how im supposed to add it exactly

proper niche
#

Well yes, but actually no.

The simplest variant would look something like this: ```cs
[SerializeField] private float stopForce = 1f;

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (movement.sqrMagnitude > 0.01f) {
rb.AddForce(movement.normalized * speed);
} else { // if there is no input
rb.AddForce(-new Vector3(rb.velocity.x, 0f, rb.velocity.z) * stopForce); // apply force opposite of the Rigidbody's current movement
}
}However, this method does not account for individual axes. For that, you would need something like this:cs
[SerializeField] private float stopForce = 1f;

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (movement.sqrMagnitude > 0.01f) {
rb.AddForce(movement.normalized * speed);
}

if (Mathf.Approximately (moveHorizontal, 0f)) { // if there is no horizontal input
rb.AddForce(Vector3.left * (rb.velocity.x * stopForce)); // apply force opposite of the Rigidbody's current movement
}
if (Mathf.Approximately (moveVertical, 0f)) { // if there is no vertical input
rb.AddForce(Vector3.back * (rb.velocity.z * stopForce)); // apply force opposite of the Rigidbody's current movement
}
}```
But even doing this would not account for local input. So if you happen to want your Rigidbody to move relative to its rotation, based on input, you will be opening up a whole other metaphorical can of worms, because you would need to take rotation into account when calculating counter force.

Not to mention, that both of the methods I presented above can (and likely will) overshoot at some point, which you would also have to fix in some way.