Simple script

Hey, I using simple script for elevator- a simple cube with empty object which function as the “upper border” of the elevator (the cube) (where it should stop rise-up to the first floor):

by using “OnTriggerStay()”, when the player standing on the cube, the cube start rising to it destination:

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

public class elevator : MonoBehaviour
{
    public GameObject Cube;

    public GameObject Player;


    private void OnTriggerStay()
    {
        Cube.transform.position += movit.transform.up * Time.deltaTime;
        Player.transform.position += nott.transform.up * Time.deltaTime;
    }
}

The problem is, that I couldnt succeed to do the inverse action, means when the player go back to the elevator in the first floor- to take him back to the ground floor.

any help with that?

One easy way would be to add a empty gameobject and place it at the first floor.

Add a transform reference “FirstFloor” to that script. Drag and drop that new empty gameObject to that field in the inspector.

Add a bool “invertDirection” to the script. During OnTriggerStay, if the elevator position is roughly the same (due to rounding there might be issues if you check for exact equality between the two), invert the invertDirection boolean.

If invertDirection is true, go down instead of up.

You might want to add another empty gameObject on the 0th floor too, and do the same thing.
You might want to wait for OnTriggerExit to invert the bool if you want the elevator not to change direction instantly but wait for the player to exit and come back, and then make the elevator to stop moving if both the direction haven’t yet been inverted and the position of the elevator is on the correct floor.

1 Like

Ijust cant find the bool “invertDirection”. I only need one floor. up and down and its just not working. im very new with that and cant find the invertDirection bool…just bring me “invoke” instead