Published on

[Unity] Door-opening gimmick

Authors
  • Evelyn

Please share this article if you like!

Hi, I'm Evelyn and I'm Indie Game Develop;er!!

It's hard to edit animation data in Unity's Animation window. On the other hand, it's too much work to create animations in Blender.

If you read this article to the end, you will be able to implement it as shown in the following image.

This article will be based on the assumption that the implementation of making the character move around has been completed.

If you do not have such a project yet, please read the following article first.

I also have a Youtube video explaining the process, so if you have trouble understanding the text alone, please watch the video as well.


Table of Contents

Importing a package

First of all, to implement opening doors, let's import the assets that have doors into our project.

You can use any asset that you normally use, but in this article we will use a free asset called Medieval house modular lite.

Then, import and place the 3D model of the house into the scene.

After that, add a Mesh Collider component to prevent the 3D character from penetrating the mesh of the 3d model.

Setting up door-opening animation

Next, create an Animator Controller and an Animation Clip for opening the door.

Next, create an Animator Controller and an animation called OpenDoor in the project window.

Double click on Animator to open the Animator window, create an Empty State and name it Idle.

Create an Empty State and name it Idle. Create a parameter called Open which is a bool type flag.

Create a Transition from the Idle State and make sure that the OpenDoor animation is executed when the Open flag is true.

Now, add an Animator component to the door game object and apply the Animator Controller that we just created.

Then edit the animation of the door opening!

If you are using the same assets as I am, please refer to the youtube video for a detailed explanation.

Coding door opening gimmick

Finally, we'll implement an animation that plays a door opening when the character enters a certain area.

Create a Cube as a child element of the door game object and increase the Scale of Transform.

Then, enable isTrigger on the Box Collider.

The character will now be able to enter the cube.

Create a C Sharp file and paste the following code into it.

OnTriggerEnter method can be used to detect the intrusion of a character, and the

DoorHandler.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorHandler : MonoBehaviour
{
    [SerializeField] Animator DoorAnimator;

    private bool _openFlag = false;

    private void OnTriggerEnter(Collider other)
    {
        if (_openFlag) return;
        _openFlag = true;

        DoorAnimator.SetBool("Open", true);
    }
}

Add the previous script to the Cube and remove the Cube's material to make it transparent.

Finally, assign the Door Animator component to the DoorAnimator variable defined as a SerializeField and you are done.

Once you have done this, you should see an animation of the door opening as shown below.

Wrap up

Running a script when a character enters a certain area can be applied to more than just opening doors, so please use it in your games! 

If you like this article, please subscribe to our youtube channel and follow us on twitter.

Summary of this article
  • Let's add an Animator component to the door game object.
  • Let's use the OnTriggerEnter method to detect character intrusion. Don't forget to enable the collider's isTrigger when doing so.

Thank you for reading to the end.

I hope you have good luck in your game development.

Please share this article if you like!