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

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.

using UnityEngine;
using System.Collections;
using System.Threading;

public class elevator : MonoBehaviour {

    public float minHeight = 0.03f;
    public float maxHeight = 100.0f;
    public float velocity = 9;
    public bool trigger;


    // Use this for initialization
    void Start()
    {
        StartCoroutine(Wait());

    }

    public IEnumerator Wait()
    {
        trigger = false;
        yield return new WaitForSeconds(5f);
        trigger = true;
    }


    // Update is called once per frame
    void Update()
    {
        float y = transform.position.y;
        if (trigger == true)
        {
            y += velocity * Time.deltaTime;
            if (y > maxHeight)
            {
                y = maxHeight;
                StartCoroutine(Wait());
                velocity = -velocity;
            }
            if (y < minHeight)
            {
                y = minHeight;
                StartCoroutine(Wait());
                velocity = -velocity;
            }
            transform.position = new Vector3(transform.position.x, y, transform.position.z);
        }
    }
}

hello, you are using " velocity = -velocity , " try using " velocity - * Time.deltatime ; " .

using UnityEngine;
using System.Collections;
using System.Threading;
public class elevator : MonoBehaviour {
    public float minHeight = 0.03f;
    public float maxHeight = 100.0f;
    public float velocity = 9;
    public bool trigger;
    // Use this for initialization
    void Start()
    {
        StartCoroutine(Wait());
    }
    public IEnumerator Wait()
    {
        trigger = false;
        yield return new WaitForSeconds(5f);
        trigger = true;
    }
    // Update is called once per frame
    void Update()
    {
        float y = transform.position.y;
        if (trigger == true)
        {
            y += velocity * Time.deltaTime;
            if (y > maxHeight)
            {
                y = maxHeight;
                StartCoroutine(Wait());
                velocity  --*Time.deltaTime;
            }
            if (y < minHeight)
            {
                y = minHeight;
                StartCoroutine(Wait());
                velocity --*Time.deltaTime;
            }
            transform.position = new Vector3(transform.position.x, y, transform.position.z);
        }
    }
}

Thanks for the suggestion. It didn’t quite work out; the way you wrote it gave me errors. I instead wrote

velocity = velocity --*Time.deltaTime

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.

Hello, I understand what happens to you, try using this code:

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour
{
    public Transform target;            // The position that that camera will be following.
    public float smoothing = 5f;        // The speed with which the camera will be following.

    Vector3 offset;                     // The initial offset from the target.

    void Start ()
    {
        // Calculate the initial offset.
        offset = transform.position - target.position;
    }

    void FixedUpdate ()
    {
        // Create a postion the camera is aiming for based on the offset from the target.
        Vector3 targetCamPos = target.position + offset;

        // Smoothly interpolate between the camera's current position and it's target position.
        transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
    }
}