Skip to content

Rigidbody2DComponent

The physics body on an entity. For raycasts and overlap checks see Physics2D; for the concepts — colliders, sensors, layers, the fixed timestep — see the 2D Physics guide.

csharp
public class Player : GameEntity
{
    private Rigidbody2DComponent? m_Body;

    protected override void OnCreate()
    {
        m_Body = GetComponent<Rigidbody2DComponent>();
    }

    protected override void OnFixedUpdate(float fixedTs)
    {
        if (m_Jump.WasPressedThisFrame && m_Grounded)
            m_Body?.ApplyLinearImpulse(new Vector2(0, 8), wake: true);
    }
}

Type

The body's BodyType. Read and write.

ValueBehaviour
BodyType.StaticNever moves. Walls, floors, level geometry — the cheapest kind
BodyType.DynamicFully simulated: gravity, forces, collision response
BodyType.KinematicMoves, but nothing pushes it. Moving platforms, scripted doors
csharp
m_Body.Type = Rigidbody2DComponent.BodyType.Kinematic;

LinearVelocity

The body's velocity in metres per second, as a Vector2. Read and write.

csharp
Vector2 v = m_Body.LinearVelocity;
if (v.Y < -20)
    PlayFallScream();

Setting velocity is usually the wrong tool

The setter exists mainly so pooled entities can be reset — a recycled bullet has to have last life's velocity cleared in OnDespawn. For gameplay movement, prefer ApplyLinearImpulse, which respects mass. Assigning velocity directly on a body you're otherwise simulating fights the simulation.

csharp
protected override void OnDespawn()
{
    GetComponent<Rigidbody2DComponent>()?.LinearVelocity = Vector2.Zero;
}

ApplyLinearImpulse(Vector2 impulse, bool wake)

Applies an impulse at the body's centre of mass. wake rouses a body that physics had put to sleep — pass true unless you have a reason not to.

csharp
m_Body.ApplyLinearImpulse(new Vector2(0, 8), wake: true);   // jump

ApplyLinearImpulse(Vector2 impulse, Vector2 worldPosition, bool wake)

The same, but applied at a specific world point — which also imparts spin. This is how an off-centre explosion sends a crate tumbling rather than sliding.

csharp
m_Body.ApplyLinearImpulse(blastDirection * force, blastCentre, wake: true);

Impulses belong in OnFixedUpdate

Applying an impulse in OnUpdate makes its effect depend on frame rate — the same jump is higher at 144 fps than at 60. OnFixedUpdate runs on the fixed physics clock, which is why it exists. See GameEntity.

SetIgnoreOneWayPlatforms(bool ignore)

Lets this body fall through one-way platforms. Set true while the player is holding down + jump; set it back to false once the drop-through is done — it does not reset itself. No effect on ordinary collisions.

csharp
private void HandleDropThrough()
{
    bool dropping = m_Down.IsPressed && m_Jump.WasPressedThisFrame;
    if (dropping)
    {
        m_Body?.SetIgnoreOneWayPlatforms(true);
        RunCoroutine(ReEnableAfter(0.3f));
    }
}

private System.Collections.IEnumerator ReEnableAfter(float seconds)
{
    yield return seconds;
    m_Body?.SetIgnoreOneWayPlatforms(false);
}

One-way behaviour itself is a Box Collider 2D setting, not a tile property — see Tilemaps if you're building levels from tiles.

See also

  • Physics2D — raycasts, overlaps, and shapecasts
  • GameEntityOnFixedUpdate, OnCollisionEnter, OnTriggerEnter
  • 2D Physics — the conceptual guide