Animation Triggers

3D door that opens and closes

Estimated reading: 4 minutes 122 views Contributors

Below is a learner-friendly worksheet that guides students step-by-step through creating a door animation and controlling it with simple triggers (like buttons or player interaction).


Unity Worksheet: Animating a 3D Door (Open and Close with Triggers)

Objective

Learn how to:

  • Animate a 3D door opening and closing.
  • Control the door using triggers and simple C# scripting.
  • Understand Unity’s Animator, Triggers, and Transitions.

Section 1: Scene Setup

1.1 Create a New Scene

  1. Open Unity and create a new 3D Scene.
  2. Save it as DoorAnimationScene.

1.2 Add a Door Object

  1. In the Hierarchy, right-click → 3D Object → Cube.
  2. Rename it Door.
  3. Scale it to look like a door (e.g., X=1, Y=2, Z=0.2).
  4. Move its Pivot (the rotation point) to one side:
    • Select the Door.
    • In the Inspector, click the Model tab (if using a model) or adjust the Position so the hinge side is at (0, 0, 0).

Tip: This ensures the door swings open like a real door.


Section 2: Create Door Animations

2.1 Create an Animator Controller

  1. Go to Assets → Create → Animator Controller.
  2. Name it DoorAnimatorController.
  3. Drag it onto the Door object (this adds an Animator component automatically).

2.2 Create Animation Clips

  1. Select the Door.
  2. Open Window → Animation → Animation.
  3. Click Create → save as DoorOpen.anim.
  4. Click the red Record button (●).
  5. Move the timeline to 1 second.
  6. Rotate the Door’s Y-axis to about 90° (like it’s opening).
  7. Stop recording.
  8. Test the animation by pressing the play button in the Animation window.

2.3 Create a Close Animation

  1. In the Animation window, click the clip dropdown → Create New Clip.
  2. Save as DoorClose.anim.
  3. Record the door rotating back to 0° on Y-axis over 1 second.

Section 3: Animator Transitions and Triggers

3.1 Add Triggers

  1. Open Window → Animation → Animator.
  2. Select your DoorAnimatorController.
  3. Click the + button in the Parameters tab → choose Trigger.
    • Name one Open.
    • Add another and name it Close.

3.2 Set Transitions

  1. Right-click DoorOpen → select Set as Layer Default State (optional).
  2. Right-click DoorOpenMake Transition → drag to DoorClose.
    • Click the transition arrow.
    • In Conditions, add Close.
  3. Right-click DoorCloseMake Transition → drag to DoorOpen.
    • Add Open as a condition.

Now your Animator is ready to switch between open and close based on triggers!


Section 4: Scripting the Triggers

4.1 Create a New Script

  1. In the Project window → Create → C# Script.
  2. Name it DoorTriggerScript.
  3. Attach it to the Door object.

4.2 Add This Code:

using UnityEngine;

public class DoorTriggerScript : MonoBehaviour
{
    private Animator animator;
    private bool isOpen = false;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // Press 'E' to toggle door open/close
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (isOpen)
            {
                animator.SetTrigger("Close");
                isOpen = false;
            }
            else
            {
                animator.SetTrigger("Open");
                isOpen = true;
            }
        }
    }
}

4.3 Test It!

  • Press Play.
  • Hit E to open and close the door.

Section 5: Adding a Player Trigger Zone

To make it feel more interactive, the door can open when the player walks near it and close when they leave.

5.1 Create a Trigger Collider

  1. Create an Empty Object called DoorTriggerZone.
  2. Add a Box Collider → check Is Trigger.
  3. Resize it to cover the area in front of the door.

5.2 Add a Trigger Script

Create a new script called DoorAutoTrigger.cs and attach it to DoorTriggerZone:

using UnityEngine;

public class DoorAutoTrigger : MonoBehaviour
{
    public Animator doorAnimator;
    private bool isOpen = false;

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player") && !isOpen)
        {
            doorAnimator.SetTrigger("Open");
            isOpen = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player") && isOpen)
        {
            doorAnimator.SetTrigger("Close");
            isOpen = false;
        }
    }
}

5.3 Setup:

  1. Create a Player object (e.g., a Capsule) and tag it as Player.
  2. Drag the Door’s Animator into the doorAnimator field of the script.
  3. Hit Play and walk into the trigger zone — the door should open when you approach and close when you leave!

Section 6: Challenges

  1. Add Sound Effects:
    Play a creaking sound when the door opens or closes.
  2. Add a Lock Mechanic:
    Prevent the door from opening until the player finds a “key.”
  3. Add an Animation Curve:
    Smooth the door’s motion using easing curves in the Animation window.

Reflection Questions

  1. What is the role of the Animator Controller in this setup?
  2. How do Triggers differ from Booleans in controlling animations?
  3. Why is the door’s pivot position important for realistic movement?
  4. How could you modify this setup to open the door with a button instead of proximity?

Share this Doc

3D door that opens and closes

Or copy link

CONTENTS

Chat Icon Close Icon