Course contentsShow
← Unity Game Development
Lesson 179 of 8283. Physics and InteractionFree lesson

Implementing Velocity-Based Horizontal Movement

Learners will manipulate the Rigidbody velocity vector to create responsive movement that respects existing physics momentum.

In this lesson, you will learn how to achieve snappy, responsive character movement by directly manipulating the Rigidbody velocity vector instead of applying forces.

Why Use Velocity?

While applying forces (like AddForce) is great for physics-driven objects like billiard balls, it often feels "floaty" or sluggish for player-controlled characters. By directly setting the velocity property of a Rigidbody, we tell Unity exactly how fast the object should move right now. This bypasses the ramp-up time of acceleration, making movement feel tight and immediate.

The Physics Workflow

The trick is to modify the horizontal components (x and z) while preserving the existing vertical component (y). This ensures that your character still obeys gravity and doesn't get stuck in mid-air when jumping or falling.

Here is how you implement this using the Rigidbody component:

private Rigidbody rb;
public float moveSpeed = 5f;
private Vector2 inputVector;

void FixedUpdate()
{
    // Maintain current vertical velocity for gravity, 
    // but overwrite the horizontal velocity
    Vector3 targetVelocity = new Vector3(inputVector.x * moveSpeed, rb.velocity.y, inputVector.y * moveSpeed);
    
    rb.velocity = targetVelocity;
}

Why FixedUpdate?

We perform this calculation inside FixedUpdate because we are interacting directly with the Rigidbody physics engine. This ensures our movement updates are synchronized with the physics timestep, preventing jitter and ensuring consistent behavior regardless of your frame rate.

Managing Momentum

Because we are setting the velocity directly, the character will stop instantly when you release the movement keys. If you want the character to "slide" or come to a natural stop, you would typically use AddForce or modify the drag property on the Rigidbody. For high-action platformers or shooters, however, this velocity-based approach is the gold standard for getting that "tight" feel that players demand.

Key Takeaway: By setting the Rigidbody velocity directly while preserving the existing y-axis value, you create responsive, frame-perfect horizontal movement that ignores acceleration lag while maintaining gravity.