Search Unity

Elevator that rises when the player steps on it

Discussion in 'Scripting' started by inaudiblefuzz, Jan 17, 2019.

  1. inaudiblefuzz

    inaudiblefuzz

    Joined:
    Nov 9, 2018
    Posts:
    12
    I'm trying to make an elevator that raises when I step on it. When I get off there's a small pause and then it goes back to its default location. Strictly vertical movement. I have been online trying to find tutorials but the good ones all have buttons. I would really like the elevator to trigger from the player without the need of a button.

    If you have any newb friendly advice / tips I would greatly appreciate them!

    Thank you.
     
  2. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    inaudiblefuzz likes this.
  3. inaudiblefuzz

    inaudiblefuzz

    Joined:
    Nov 9, 2018
    Posts:
    12
    Thank you for taking the time to respond. To be honest it's like reading another language. : /

    I would add a box collider to my platform. Check on trigger?

    What actually triggers the vertical movement, speed, height?
     
  4. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    I would add an box collider to the elevator GameObject, be sure to check "Is Trigger".
    Now, you will have to add a simple script to elevator GameObject, something like "ElevatorManager" and make the logic there. So, you want to move the elevator when player steps into it (which is really weird idea, because player could exit it quickly so the elevator would move without him).
    To check if player stepped into the elevator, you will have to check it in the ElevatorManager script
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.tag == "Player")
    4. {
    5.      //Code to move the elevator
    6.      //It's good idea to always check if part of the code is running, so you could add a simple      Debug here to make sure it collides
    7. Debug.Log("Player entered elevator");
    8. }
    9. }
    That's it. To delay movement of elevator you can make a simple timer by subtracting Time.deltaTime from some float variable, but I think Coroutines is better idea. Is it more clear now?
     
    inaudiblefuzz likes this.
  5. inaudiblefuzz

    inaudiblefuzz

    Joined:
    Nov 9, 2018
    Posts:
    12
    Thank you!! It makes a lot more sense. I'm going to give a whirl right now.
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If it's that bizarre to you i'd suggest following some basic tutorials on both C# as a language and Unity itself.
    You would add a box collider to your platform(That already has a collider where you need to stand obviously), mark it as a trigger(there's a check box in the component menu) so that it won't block you actually going on the platform, set it where the player will stand.

    What triggers is an event/callback, a message basically that sends out a signal to any script listening, it's up to you to do stuff after.

    If you want to go from X to Y in T seconds, you can use a lerp function, if you want to wait a certain time after the player exits, you can use "WaitForSeconds" or "WaitUntil" in a coroutine.

    BTW, @qkson you can make OnTriggerEnter a coroutine, just in case you didn't know.
     
    qkson and inaudiblefuzz like this.
  7. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    I did not know that, thanks :)
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can also do it to Start and a bunch of others, I can't find any page stating what callbacks support this though, any body else got a link?
     
    inaudiblefuzz likes this.