Sci-Fi Styled Modular Pack - How to make door to work

Mar 17, 2019 Unity


Lots of people complain that doors in the Sci-Fi Styled Modular Pack are not working. Although it's true that just after package import, the door not working with the character, all you need to do is write a simple script which will operate the Animator component of a door. So, here how it's done :)

1. Make the door work again!

I assume you have already imported the package.

First, we need to add a door to the scene.

Unity Editor with door

On Inspector Window add a new C# script.

Inspector window

Open it with your code editor and paste this code.

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

public class DoorController : MonoBehaviour {

    public GameObject character;
    public float distance = 10f;
    private Animator animator;

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

    // Update is called once per frame
    void Update () {
        if (Vector3.Distance(character.transform.position, transform.position) <= distance)
        {
            animator.SetBool("character_nearby", true);
        }
        else
        {
            animator.SetBool("character_nearby", false);
        }
    }
}

Save it and after Unity compile, place your character object to character property in new created C# script.

Door controller with assigned Character

And that's it! Door now open when your character is nearby!


2. Why this script is not included in package?

It's an easy script and the door shouldn't always open when you approach it. Games have buttons that open something, puzzles etc. I don't want to limit or interfere with those implementations.