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);
}
}
}