We are going to tackle creating a modular elevator. This elevator will move between floors after a time delay.
We create two empty game objects called Waypoints that will be the markers for the elevator to travel between.
We create an Elevator script and attach it to the Elevator Platform game object.
We will declare a few variables…
- An array of Transforms called _wayPoints that will hold the position of our Waypoints.
- An int called _targetID that will hold, which Waypoint we are moving towards in the array.
- A float that will be for determine how fast the elevator will move between Waypoints.
In the Inspector we assign the Waypoints to the array and make sure they are in numerical order.
We are going to set the target ID (0) to the current value of target ID (0) plus 1, so target ID is now 1. We divide by this by the length of the Waypoints array (2), so target ID is now 0 with remainder of 1/2. The % returns the remainder, which is rounded to 1. The target ID is equal to 1.
In a Fixed Update, we will set the position of the Elevator to Vector3.MoveTowards function. This requires the current position of the object moving, the position of where you are moving to, which is the Waypoint in the target ID position of the array, and the speed this object should move towards the end position.
We want to check when the position of the Elevator is equal to the Waypoint we are moving to and when this happens we will use our start code. We set the target ID to the current value of target ID (1) plus 1, so it is 2. This is divided by the length of the Waypoint array 2, so it is 2 divide by 2 which equals 0. The % returns the reminder which there isn’t any, so the target ID is 0. This will now have the Elevator move towards Waypoint 0 positions.
We are going to add a delay, so the Elevator sits at level for a certain amount of time before moving.
We are going to add two new variables…
- A float variable called _elevatorDelay that will determine how long the elevator will wait before moving again.
- A bool variable called _moving that will track whether the Elevator is currently moving.
We will only be able to move when the _moving variable is true, which it is at the start, but we will need a way to set it false.
Using a coroutine we will set _moving false then wait a certain amount of seconds using the _elevatorDelay float variable before setting it back to true.
When do we want to start this coroutine? We want to start it after we have changed the target ID, so _moving is false which stops the MoveTowards and it will wait a set amount of seconds before _moving is set true.
This works great! But, we run into an issue when the player stands on the elevator.
You will notice when the player is standing on the elevator it will jitter. Lets fix this.
Fixing the Jitter
Lets clean up the Elevator by parenting the elevator platform and the waypoints to an empty game object, so everything is neatly housed in one place.
We are going to add a second Box Collider to the elevator platform and we will resize it so it fits the player in it. Also, make sure this Box Collider has Is Trigger true.
In the Elevator script, we will use OnTriggerEnter and OnTriggerExit to detect when the player has entered and exited the Box Collider. We use the Collider other to check the tag. If it is the Player entering the Box Collider we set Player’s parent transform to the elevator. This allows it to move with the elevator. When the Player exits, we simply set its parent transform to nothing.
This Elevator script is modular and easily prefabbed. We can use it to make moving platforms different from an elevator, which I will cover next. Until then…Cheers!