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

Designing Physics-Driven Traps and Hazards

Develop dynamic environmental hazards that utilize velocity-based triggers and joint-driven movement to challenge player navigation.

In this lesson, you will learn how to transform static obstacles into dynamic threats by utilizing velocity-based triggers and mechanical joints to create reactive environmental hazards.

Leveraging Physics for Traps

Static obstacles can feel predictable, but by using the Rigidbody component, we can create traps that react to the player's presence. Instead of relying on pre-animated sequences, we use Hinge Joints or Configurable Joints to create pendulums or rotating spikes that swing realistically. When the player enters a Trigger Volume placed near these hazards, you can apply a sudden force or change the joint's motor settings to activate the trap.

Velocity-Based Triggers

A trap is most effective when it responds to the player's momentum. You can detect how fast a player is moving through a zone by checking the velocity magnitude of their Rigidbody. For example, a heavy gate can be set to drop only when the player's horizontal velocity exceeds a certain threshold, simulating a "pressure plate" reaction.

Consider this logic for a pendulum trap triggered by a proximity check:

void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")) {
        // Apply an impulse force to start the pendulum swinging
        Rigidbody rb = GetComponent<Rigidbody>();
        rb.AddForce(Vector3.right * 10f, ForceMode.Impulse);
    }
}

Creating Mechanical Hazards

To build a rotating spike hazard, attach a Hinge Joint to a pivot point. In the Inspector, enable the Motor setting. By toggling the useMotor property via script when the player enters a Trigger Volume, the spikes will suddenly spin at high velocity, creating an immediate navigation challenge. Because these are physics-driven, the spikes will naturally collide with the player, pushing them back or dealing damage based on their collision velocity.

By using FixedUpdate to manage these forces, you ensure that your traps behave consistently regardless of frame rate, keeping your hazards feeling fair but lethal. Always ensure your hazards have Continuous Collision Detection enabled if they move at high speeds to prevent them from "tunneling" through the player's collider.

Key Takeaway: By combining joint-driven mechanics with velocity-based force application, you can create immersive hazards that respond dynamically to player movement rather than relying on scripted animations.