Search Unity

Simple script

Discussion in 'Scripting' started by e1e1e1, Aug 15, 2019.

  1. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class elevator : MonoBehaviour
    6. {
    7.     public GameObject Cube;
    8.  
    9.     public GameObject Player;
    10.  
    11.  
    12.     private void OnTriggerStay()
    13.     {
    14.         Cube.transform.position += movit.transform.up * Time.deltaTime;
    15.         Player.transform.position += nott.transform.up * Time.deltaTime;
    16.     }
    17. }
    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?
     
  2. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    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.
     
    e1e1e1 likes this.
  3. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    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