1

I'm a very new game developer, and I was making a simple first-person script using AddRelativeForce. However, when I go forward(or any direction), I slowly accelerate instead of going at a constant speed. Does anyone know how to fix this?

Donkey45
  • 21
  • 3

2 Answers2

1

Ok, I found something that works. I basically kept my entire old script, but in every frame I set the velocity to 0 and then added the relative velocity. This caused AddRelativeVelocity to essentially be the same thing as SetVelocity, but instead it set the relative force. While this worked in my situation, it might not work in others. Thank you to all of the helpful people who answered my question, though!

Pikalek
  • 12,372
  • 5
  • 43
  • 51
Donkey45
  • 21
  • 3
  • If you just want to set the velocity, don't add a force, just set the velocity. If you want to make it relative to the object's orientation, just transform the vector first: body.velocity = body.rotation * localVelocity; – DMGregory Feb 15 '24 at 00:33
0

If you want to move your character using the AddRelativeForce-Method you need to keep track of the current velocity to prevent applying a force when the limit is reached. Something like this could be a solution:

public float yourMaximumMovementSpeed = 10f;

// we use velocity.magnitude to get the length of the vector if (rigidbody.velocity.magnitude < yourMaximumMovementSpeed) { rigidybody.AddRelativeForce(yourDirection * yourForce); }

Instead of adding force you also could just set the velocity directly:

rigidbody.velocity = yourMovementVector * yourMovementSpeed;