making elevator go back down if player falls off structure.

Hi, I’ve got an elevator that goes up and down if a button is pressed, but I’m trying to put some lines into the code so that if the player falls or jumps off the structure, the elevator will return to its initial place, by playing the DOWN animation, so the player can go back up.

The y transform position of the player when they go up the structure is 16, so I thought that if the player’s y position was less than that after the UP animation had been played, that would indicate the player had fallen or jumped from the structure.

Whilst the code is running, the results are weird and not what I’m trying to do.

Any insight would be greatly appreciated.

PS, this isn’t the whole code, but the part that I believe pertains to my problem.

public Transform player;
    public float playerPosition;
    public float heightPosition = 16f;
    private bool pressedButton = false;
    private bool isElevatorUp = false;
  

    void Update()
    {
        {
            if (player.position.y < heightPosition)


            {
                if (isElevatorUp == true)
                {
                    DOWN();
                }

            }

        }
    }
void DOWN() {

        target = GameObject.Find("ELEVATOR");
        target.GetComponent<Animation>().Play("DOWN");
    }

use triggers, OnTriggerEnter and OnTriggerExit.

1 Like

You could solve it by level design instead of code. Simply put a call button outside the elevator to request it to come down. That’s how real elevators work.

2 Likes

That’s a good point.

Thanks everyone for their feedback.