How do you apply drag to your NPCs?

Godot Version

4.7.2 Stable

Question

I was building some NPC steering behaviors recently and wondered how to implement drag. The NPCs extend CharacterBody3D, although I am not sure they should because I saw this in the documentation of them:

Now in RigidBodies you have linear_damp property which I think functions like a drag but not 100% sure on that.

Would you use some custom math for the drag inside the CharacterBody3D like this i found online: dragForce = (force/dragCoefficient) / (maxSpeed^2), or would you just switch the NPCs to RigidBody3D and call it a day? I am kind of scared of moving the NPCs to the latter because i lose the ability to use move_and_slide() but curious to hear what you guys think.

Sort of, yes. It’s just like the rest of your game; you have to implement it. The actual equation you should use will differ slightly since you’re not dealing with an object that has a mass. However, you could always give your NPCs a mass characteristic, if that is how you want your system to function.

Here is a simple way to do it:

# CLASS: CharacterBody3D
@export var drag = 1.0

func _physics_process(float delta):
    # Project velocity onto xz-plane to avoid applying drag to the Y-axis
    var vel_xz = velocity.slide(Vector3.UP)
    # Compute drag as a percentage of the current velocity
    var drag_amount = vel_xz * drag
    # Apply drag over time (via delta)
    velocity -= drag_amount * delta

Try it out with your own equation as well.

For the majority of cases, CharacterBody3D is simpler to work with because it, as stated in the docs, is specialized for user-controlled objects (user also refers to NPCs). That said, it all depends on the end result you’re looking to achieve.

Take Rocket League as an example. That game is entirely physics-based. The wheels on the cars, their boost, and the ball – it’s all physically simulated via forces. How you hit the ball with your car matters. The game simply wouldn’t be the same without the use of rigid-bodies. In contrast, games that don’t require a physical simulation are much better off using classes that provide more control over the object’s motion (such as CharacterBody3D). Then you don’t need to work against the outcome of the physics simulation and you can focus on creating a tight-knit bounded system.

I’d say the main difference between the two classes is the use of forces.

RigidBody3D is influenced (moved) by forces.
CharacterBody3D is moved directly via a specialized velocity variable.


The short answer to your question is: it depends. I don’t know what your game is so I can’t definitively recommend one class over the other.

Should you have any other questions, feel free to post them here.