- Home /
Making elevator stop at triggers
I'm trying to make an elevator stop at different triggers I've set up to act as floors. However, not matter what I do with my code, the elevator passes through (despite me setting the "Is Trigger" Boolean value to true).
The code I'm using is a modified version of a script I found online that was a good workaround for using rigidbody components to move the elevator up and down with force. Currently, it uses an int value to dictate which direction the elevator moves in an Update function. I have the E key switch the int value.
Unfortunately, my main issue is with it detecting the triggers in a meaningful way. I've tried bool values, OnTriggerEnter(), and OnTriggerStay(), and none of them worked.
I must be missing something simple, as the triggers "work": I used a bool value to detect whether the triggers were being entered, and they always came up "true." Hopefully, the code can explain itself better than I can:
public class ElevatorMovement : MonoBehaviour {
public int dirPlat;
float frameMultiplyer = 2.5f;
float frameSpeed;
void Update()
{
frameSpeed = frameMultiplyer * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.E))
{
//change direction of elevator movement
if (dirPlat == 0)
dirPlat = 1;
else
dirPlat = 0;
}
//goes up
if (dirPlat == 0)
transform.Translate(Vector3.up * frameSpeed);
//goes down
if (dirPlat == 1)
transform.Translate(Vector3.down * frameSpeed);
//stops elevator
//if (f == true) {
// transform.Translate(Vector3.down * 0);
//}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "ElevatorStop") {
transform.Translate(Vector3.down * 0);
}
}
}
If anyone has suggestions for what I can do to solve the trigger dilemma, I'd love to hear them.