Demystifying Velocity Vs. Addforce: When And Why To Use Each For Player Motion In Unity
The Core Difference Between Velocity and AddForce
The key difference between setting a Rigidbody’s velocity directly versus using AddForce lies in the type of motion control you need. Velocity allows immediate, direct, and continuous control over speed and direction. AddForce applies a physical force that accelerates the Rigidbody over time according to its mass.
Velocity for Direct Manipulation
Using Rigidbody.velocity equals direct and absolute control over the speed and direction of a game object. It allows you to dictate motion instantly. This makes it ideal for direct player control schemes like keyboards, joysticks, or pointer input.
AddForce for Physical Interactions
AddForce allows momentum, acceleration, and physical materials to influence motion. Forces apply over time based on mass. This makes AddForce perfect for physical interactions between game objects like collisions, explosions, or gravity.
How Physics Works in Unity
Understanding the basics of how Unity handles physics helps clarify when to use velocity versus AddForce.
Rigidbodies and Forces
Rigidbodies enable objects to receive and apply physical forces. AddForce applies a force to a Rigidbody over one physics update. Without forces, rigidbodies do nothing. AddForce breath life into motion.
Mass Affects Acceleration
The acceleration a Rigidbody experiences from a given force depends on its mass. Lighter objects accelerate faster from the same AddForce. Mass consideration helps tuning the right forces.
Momentum Conserves Velocity
Without forces, rigidbodies maintain velocity over time due to momentum. A fast moving Rigidbody continues moving quickly unless slowed by drag, friction, or opposing impulses.
Setting a Rigidbody’s Velocity Directly
Setting velocity overrides physics to directly dictate the speed and direction of a Rigidbody. This gives you immediate and complete control which is ideal for player input control.
Bypassing Physics
Velocity ignores momentum, mass, drag, friction, and forces. It teleports objects through space instantly. This lacks realism but enables direct motion control.
Continuous Motion Control
Since physics forces don’t affect velocity, you can set it continuously in updates loops to provide frame-by-frame direction/speed control. This suits mouse, keyboards, thumbsticks.
Examples of Using Velocity
Typical examples where directly setting velocity excels include:
- Controlling a player spaceship via keyboard or virtual thumbstick
- Moving an AI monster directly toward the player
- Simulating mouse, touchscreen, or other pointer-based input
- Propelling bullets towards targets
Applying Forces Over Time with AddForce
AddForce applies a physical force that pushes rigidbodies over time based on their mass. This allows momentum and acceleration to influence motion.
Gradual Acceleration
Forces increase a rigidbody’s velocity gradually according to its mass, rather than instantly changing it. Lower mass objects accelerate faster.
Physical Material Interactions
Surface friction, bounciness, and other physical materials alter forces and motion. AddForce leverages these material simulations.
When to Use AddForce
Good examples of when to apply AddForce include:
- Simulating explosions adding radial impulses
- Applying downward gravity over time
- Tossing rag-doll physics objects
- Batting physics objects with baseball bats
- Firing cannon balls and other projected objects
Deciding Which to Use
Choosing between velocity and AddForce depends mainly on if you need direct control or physical realism.
Prefer Velocity for Control
In cases where you need direct and continuous manipulation of motion, such as player control, use Rigidbody.velocity.
Prefer AddForce for Realism
For simulations leveraging physical materials and momentum, use AddForce and other physics forces rather than setting velocity.
Consider Combining Both
You can also mix both velocity control and AddForce in the same game. Use velocity for player input while the environment applies AddForces.
Code Examples Showing Velocity vs. AddForce
Here is example code contrasting updating velocity manually versus applying forces.
Velocity Code Example
void Update () { // Get player input axis float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); // Set rigidbody velocity directly rigidbody.velocity = new Vector3(h * speed, rigidbody.velocity.y, v * speed); }
AddForce Code Example
void FixedUpdate () { // Get player input axis float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); // Accelerate rigidbody using AddForce rigidbody.AddForce(new Vector3(h * speed, 0.0f, v * speed)); }
Common Mistakes and How to Avoid Them
Here are some common velocity versus AddForce errors and tips on avoiding them.
Applying Forces Without Rigidbodies
Don’t apply AddForce to game objects without Rigidbodies. This silently fails. Always check for a Rigidbody first.
Forgetting to Limit Velocities
Without max limits, velocity can accumulate runaway speeds. Clamp magnitudes after updates.
Simulating Physics Without Forces
Attempting to simulate physics by manually changing velocities ignores momentum and mass. Leverage forces instead.
Additional Considerations
Some other best practices help maximize performance with velocity and forces.
Minimize Collision Checks
Continuously setting velocity can require excessive collision checks hurting performance. Manage layers, triggers, and intervals.
Use FixedUpdate with Forces
Perform AddForce calls inside FixedUpdate for better force application timing.
Balance Realism with Performance
More complex physics requires more computation. Profile and find the sweet spot balancing realism needs with available processing throughput.