Search Unity

Object moving up and down by OnTrigger

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

  1. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    Im very new in Unity and trying to make a simple box moving up when the player stand on it and its working:

    Code (CSharp):
    1. public class elevator : MonoBehaviour
    2. {
    3.     public GameObject movit;
    4.  
    5.     public GameObject nott;
    6.  
    7.     public float speed = 0.05f;
    8.  
    9.     private void OnTriggerStay()
    10.     {
    11.         movit.transform.position += movit.transform.up * speed;
    12.         nott.transform.position += nott.transform.up * speed;
    13.     }
    Now I trying to add a command- when the box reach to the first floor, its waiting to the player and when the player stand on the box again it take him back down and then repeat..

    the problem is that I just dont secceed to make the moving back down scripte, I just made another box on the first floor with (-): += - movit.transform.up...
    and I need only one object noving up and down reacting to trigger, can someone help me with that?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This box will just jump up while you staying in trigger. Instead of "fixing" that by setting speed to extremely low values, you must understand what trigger stay function happens multiple times per second and do

    Code (CSharp):
    1. movit.transform.position += Time.fixedDeltaTime * movit.transform.up * speed;
    Because Time.fixedDeltaTime shows you how much time passed since previous frame.

    And this is not the way to make lifting platform at all. You want to move platform up while characters stays on it and down when he leaves. You need to use on trigger enter and exit functions a variable for current destination.

    When player enters the trigger, check you position and choose destination. When player exits trigger, check and change destination if needed. In Update function move to current destination using Time.deltaTime, not fixedDeltaTime, since fixed is for fixed update only.
     
  3. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    I just change it from DeltaTime to speed cause I wanted it to move faster but thanks for this fix.
    so you saying this script is actually irrelevant and I need to start with new script?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  5. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    Thank you.
    Isaw these tutorials. the problem is that im using VR platform and there is a lack of tutorials about elevator platforms without bottons.
    floating object not fit to my game.

    I made an empty object used as the "border" of the elevator, I useing the simple script above with trigger to make the cube lift when the player on it. cant do the opposite direction when the player on the first floor and wanted to get back down with the same elevator (cube)
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    VR is totally pretty new thing and it will not be easy if you're begginer game developer. I haven't worked with VR so can't help a lot but hey may be you can have two triggers for lift at the top and bottom and let those triggers change lift destination instead of bounding box?
     
  7. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    jusy not working. the only thing I made is 2 empty objects with 2 cubes, one is for up, when the player stand on the cube:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class elevator : MonoBehaviour
    6. {
    7.     public GameObject movit;
    8.  
    9.     public GameObject nott;
    10.  
    11.     public float speed = 3f;
    12.  
    13.     private void OnTriggerStay()
    14.     {
    15.         movit.transform.position += movit.transform.up * speed;
    16.         nott.transform.position += nott.transform.up * speed;
    17.     }
    18.  
    19. }
    and another on to go back down:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class elevator : MonoBehaviour
    6. {
    7.     public GameObject movit;
    8.  
    9.     public GameObject nott;
    10.  
    11.     public float speed = 3f;
    12.  
    13.     private void OnTriggerStay()
    14.     {
    15.         movit.transform.position += -movit.transform.up * speed;
    16.         nott.transform.position += -nott.transform.up * speed;
    17.     }
    18.  
    19. }
    dont know how to combine them to one script, one cube and one empty object
     

    Attached Files:

  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Code (CSharp):
    1. public class elevator : MonoBehaviour {
    2.     public Transform target;
    3.     public float speed;
    4.  
    5.     private void Update() {
    6.         if (target != null) {
    7.             if (Vector3.Distance(transform.position, target.position) > speed * Time.deltaTime) {
    8.                 transform.position += Time.deltaTime * speed * (target.transform.position - transform.position).normalized;
    9.             }
    10.             else {
    11.                 transform.position = target.position;
    12.             }
    13.         }
    14.     }
    15. }
    16.  
    17. public class elevatorTrigger : MonoBehaviour {
    18.     public elevatorTrigger next;
    19.     public void OnTriggerEnter(Collider other) {
    20.         var el = other.GetComponent<elevator>();
    21.         if (el != null) {
    22.             el.target = next.transform;
    23.         }
    24.     }
    25. }
    Haven't tested but should work.
     
  9. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    where should i put every script? when i put the elevatorTrigger in the empty object and the elevator script to the cube, the cube just floatin to the center of the second floor as I press "play" without any move from my side. same when I put both of the scripts in the cube
     
  10. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Put elevator on the moving part and triggers to both endpoints. Triggers needs colliders on them. Triggers need to be interconnected, next reference must be set in trigger inspector.
     
  11. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    still its just start moving upward as I press "play"
    you can see here:

    https://ufile.io/vezbc7eh

    this is the package of the scene
     
  12. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  13. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
  14. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    2kb scripts, 4kb scene data, remaining 18 may be zip header? idk
    i removed everyting what is not needed to make platform go up and back down
    this should solve your issue on how to make platform go back down
    the only thing you need to do is to enable/disable elevator when player enters or leaves it
    my scene does not contain any player

    actually this platform script lets you create platform moving between any number of locations
     
  15. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    Ok thank you very much for the help!