- Home /
Unity 3d Elevator Script Not Working Properly
Hello all, I'm working on an elevator script that is built off of the lift example in the official tutorials. I want my elevator to move up and down, stopping for several seconds at the top and bottom for the player to walk in and out.
However, I cannot get the elevator to stop during the timer. What's more, the player falls through the elevator when he walks in. Can anyone give me any tips or answers?
Thanks
using UnityEngine;
using System.Collections;
public class Elevator : MonoBehaviour
{
public float timeToDoorsClose = 1.4f;
public Vector3 pointB;
public float liftSpeed = 3f;
private GameObject player;
private bool playerInLift;
private float timer;
void Awake ()
{
player = GameObject.FindWithTag ("Player");
}
IEnumerator Start ()
{
var pointA = transform.position;
while (true) {
yield return StartCoroutine (MoveObject (transform, pointA, pointB, 3f));
yield return StartCoroutine (MoveObject (transform, pointB, pointA, 3f));
}
}
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i = 0.0f;
var rate = .05f / time;
while (i < 1.0f) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp (startPos, endPos, i);
//transform.Translate(endPos * liftSpeed * Time.deltaTime);
yield return null;
//timer += Time.deltaTime;
/*if (timer > 0) {
thisTransform.position = endPos;
} else {
thisTransform.position = startPos;
}*/
}
}
void Update ()
{
if (playerInLift)
LiftActivation ();
if (transform.position = pointB) {
timer += Time.deltaTime;
}
if (timer < timeToDoorsClose)
rigidbody.isKinematic = true;
else
rigidbody.isKinematic = false;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject == player)
playerInLift = true;
}
/* void OnTriggerEnter ()
{
timer += Time.deltaTime;
}
*/
/* void OnTriggerExit (Collider other)
{
if (other.gameObject == player) {
playerInLift = false;
timer = 0;
}
}
*/
void LiftActivation ()
{
player.transform.parent = transform;
//transform.Translate(Vector3.up * liftSpeed * Time.deltaTime);
}
}
bump
I really need help so if just one person can push me in the right direction it would be of great assistance
Your answer
Follow this Question
Related Questions
Bullet Sometimes Go Throw Object 0 Answers
Putting variable into new Unity GUI InputField and then Acessing that variable 1 Answer
Elevator that parents player to itself causes jittery motion. 1 Answer
Rigidbody +parenting +rotation +elevator = Player unwanted behavior(pushed away) 1 Answer
Making a sped up timer in unity 1 Answer