Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SteamVR: Up with the elevator...but no down

Discussion in 'AR/VR (XR) Discussion' started by Ghoststrider, Sep 21, 2016.

  1. Ghoststrider

    Ghoststrider

    Joined:
    Sep 21, 2016
    Posts:
    3
    So here's my situation. I'm learning how Unity and VR works, using the HTC Vive, so I create a tower and an elevator on the side. I fumble around a bit, reading some books on Safari Books, before figuring out I need to add a Rigidbody (no use gravity -- that made me fall through the floor [edit: no, it makes my view slowly tip over sideways and then fall over completely] -- and not kinematic) and a Capsule Collider to my Camera Rig so it can interact with the elevator. Oh what joy it was to be able to travel up with the elevator to an impressive height.

    Yet, when the elevator goes back down...I'm left floating in midair. The platform just falls out from under me. So I'm trying to figure out how Unity treats vertical travel. Is it a setting with gravity? Do I need to write a script to "stick" the Camera Rig to the elevator? Obviously I'm not going to just automatically fall because I'm standing on the ground in meatspace, but I had figured the Camera Rig would stay with the elevator. Obviously, I was incorrect.

    Here is my script for the elevator, which is really the only script I'm using besides the teleport script.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Threading;
    5.  
    6. public class elevator : MonoBehaviour {
    7.  
    8.     public float minHeight = 0.03f;
    9.     public float maxHeight = 100.0f;
    10.     public float velocity = 9;
    11.     public bool trigger;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         StartCoroutine(Wait());
    18.  
    19.     }
    20.  
    21.     public IEnumerator Wait()
    22.     {
    23.         trigger = false;
    24.         yield return new WaitForSeconds(5f);
    25.         trigger = true;
    26.     }
    27.  
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         float y = transform.position.y;
    33.         if (trigger == true)
    34.         {
    35.             y += velocity * Time.deltaTime;
    36.             if (y > maxHeight)
    37.             {
    38.                 y = maxHeight;
    39.                 StartCoroutine(Wait());
    40.                 velocity = -velocity;
    41.             }
    42.             if (y < minHeight)
    43.             {
    44.                 y = minHeight;
    45.                 StartCoroutine(Wait());
    46.                 velocity = -velocity;
    47.             }
    48.             transform.position = new Vector3(transform.position.x, y, transform.position.z);
    49.         }
    50.     }
    51. }
    52.  
    53.  
     
    Last edited: Sep 21, 2016
  2. Martinez-Vargas

    Martinez-Vargas

    Joined:
    Jun 24, 2016
    Posts:
    39
    hello, you are using " velocity = -velocity , " try using " velocity - * Time.deltatime ; " .
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Threading;
    5. public class elevator : MonoBehaviour {
    6.     public float minHeight = 0.03f;
    7.     public float maxHeight = 100.0f;
    8.     public float velocity = 9;
    9.     public bool trigger;
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         StartCoroutine(Wait());
    14.     }
    15.     public IEnumerator Wait()
    16.     {
    17.         trigger = false;
    18.         yield return new WaitForSeconds(5f);
    19.         trigger = true;
    20.     }
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float y = transform.position.y;
    25.         if (trigger == true)
    26.         {
    27.             y += velocity * Time.deltaTime;
    28.             if (y > maxHeight)
    29.             {
    30.                 y = maxHeight;
    31.                 StartCoroutine(Wait());
    32.                 velocity  --*Time.deltaTime;
    33.             }
    34.             if (y < minHeight)
    35.             {
    36.                 y = minHeight;
    37.                 StartCoroutine(Wait());
    38.                 velocity --*Time.deltaTime;
    39.             }
    40.             transform.position = new Vector3(transform.position.x, y, transform.position.z);
    41.         }
    42.     }
    43. }
     
  3. Ghoststrider

    Ghoststrider

    Joined:
    Sep 21, 2016
    Posts:
    3
    Thanks for the suggestion. It didn't quite work out; the way you wrote it gave me errors. I instead wrote

    Code (csharp):
    1.  
    2. velocity = velocity --*Time.deltaTime
    3.  
    But when I did that for both up and down, an amusing thing happened where my camera rig rolled right off the elevator as it was going up, and I was left spinning in the air, rolling over and over again away from my scene. A little nauseous but I found it quite hilarious. I may have misunderstood your suggested tweak.

    When I used it only for up, the elevator never went back down; and when I used it only for down, I got the same problem I had at the beginning.

    I'm wondering if I just need to apply gravity to the camera rig's rigidbody, and that there's a way to do it that doesn't cause it to fall over and act bizarrely.
     
  4. Martinez-Vargas

    Martinez-Vargas

    Joined:
    Jun 24, 2016
    Posts:
    39
    Hello, I understand what happens to you, try using this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour
    5. {
    6.     public Transform target;            // The position that that camera will be following.
    7.     public float smoothing = 5f;        // The speed with which the camera will be following.
    8.  
    9.     Vector3 offset;                     // The initial offset from the target.
    10.  
    11.     void Start ()
    12.     {
    13.         // Calculate the initial offset.
    14.         offset = transform.position - target.position;
    15.     }
    16.  
    17.     void FixedUpdate ()
    18.     {
    19.         // Create a postion the camera is aiming for based on the offset from the target.
    20.         Vector3 targetCamPos = target.position + offset;
    21.  
    22.         // Smoothly interpolate between the camera's current position and it's target position.
    23.         transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
    24.     }
    25. }