Environment Triggers

For this article I will talk about Environment Triggers.

What is an Environment Trigger?

A environment trigger is an area that is placed in a scenario for execute an action when passes through a certain area.

For this article I will make two examples of the environment triggers, a for drop an object, and other for turn lights on and off.

So for our first trigger an object will dropped when your player enters the trigger area, done that this he will reposition the dropped object three seconds that the user exits the area.

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

public class FirstTrigger : MonoBehaviour
{

    private bool playTimer;
    private float timer;

    public Rigidbody rigidbodyObjectThatWillDrop;
    public Transform transformObjectThatWillDrop;
    public Transform beginPositionOfObject;

    public float maxTimer;

    private void Update()
    {
        if (playTimer)
        {
            timer += Time.deltaTime;

            if (timer >= maxTimer)
            {
                timer = 0;
                playTimer = false;
                ResetPosition();
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            rigidbodyObjectThatWillDrop.useGravity = true;
            timer = 0;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
            playTimer = true;
    }

    void ResetPosition()
    {
        rigidbodyObjectThatWillDrop.useGravity = false;
        transformObjectThatWillDrop.position = beginPositionOfObject.position;
    }

}

As you can see this is very simple, you only detect the enter and the exit of the player through the methods OnTriggerEnter and OnTriggerExit and after this, execute your actions.

Our Second Trigger will switch off the lights, when the user enters, and when the user exits the lights will to switch on.

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

public class SecondTrigger : MonoBehaviour
{

    public Light[] environmentLights;

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.CompareTag("Player"))
        {
            for (int i = 0; i < environmentLights.Length; i++)
                environmentLights[i].enabled = false;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            for (int i = 0; i < environmentLights.Length; i++)
                environmentLights[i].enabled = true;
        }
    }

}

How can we see environment triggers concept is very simple and through this concept you is able to create area interactions for your game.

You can see the environment assembly and source code through the project on Github.

Follow-me on Instagram for stay knowing when I create new posts.

You can also leave your feedback, and your suggestion, I will read all, and I will answer for you.

Thank you for reading.