Level node positioning
Now that we have our movement across our path set up, it is time to place in our level nodes and make the player position on them correctly.
Adding the level nodes
For this tutorial I'll be using my own level nodes I've created in Blender, but if you want to use your own that is also fine.
Now lets put two of our level nodes in the scene and then position both path ends to each level node. The end result should look something like this.
Added components
Now we need to add a box collider to our level nodes and adjust its size. We have to make sure that the collider is centered on the object and that the collider sticks out a bit on the top. How big you want it to make is all up to you as long as you've centered the box collider.
The last thing we need to check is the "Is Trigger" checkbox on the box collider to turn it into a trigger area.
Positioning the player
Now that we have everything set up in Unity it's time to make the player position on the level node correctly via script. To do this we go to our path navigator script and add a "Private void OnTriggerEnter(Collider other)" to it.
Next we need to create a new coroutine function with a parameter of transform. This transform will represent the level node the player is currently entering when coming off the path. You can ignore the red line underneath the function, we'll fix that in a moment.
First we create a while loop that keeps going until the player is on the same position as the level nodes center position. In this while loop we need to return our players position for the check.
Right now the position of the player doesn't change so it will keep returning the same value over and over again, creating an infinite loop. To fix this we need to update our player's position before return it. We are going to do that by making use of the vector3 "Move Towards" function provided by Unity.
This function needs a vector3 value of its current position, a target position and a float value to determine how fast the object needs to move towards its target position.
So we can give this information to the function by providing it with our own position, the position of the level node and for the speed we can add "Time.DeltaTime" for the float value.
This technically works, but the player will now move instantly towards our targeted position instead of doing it smoothly. To fix this we create a new private float value that will handle our speed moving towards our level node. Make sure to add "[SerializeField]" to it so you can adjust the value in the inspector.
Back to our function, we only have to add "* positioningSpeedOnLevelNode" after "Time.DeltaTime". In the end your function should look like this.
Now go back to your on trigger enter function to start the coroutine we just created. Before we do that we first call the function "StopAllCoroutines();". What this does it stops all the coroutines that are currently running inside the script. We do this now, because later we'll be moving the player across the path and once the player enters the trigger area of the level, the player needs to stop moving along the path and move towards the level node.
Now that we've stopped all the coroutines we need to start our coroutine that lets us position the player on the level node. We do this by simply adding "StartCoroutine(PositionPlayerOnLevel());" to it. But we also need to provide this function with a transform parameter. We can do this by adding "other.transform" between the round brackets. In the end your function should look like this.
One last thing we need to do is in our "PositionPlayerOnPath" function removing the "StartCoroutine(MovePlayer());". The function will then end up looking like this.
Now the player positions itself on the level node correctly, but he's not facing the camera.
To fix this we need to go back into our positioning on level coroutine and add some checks and adjustments.
First, we need to check if our Y rotation is either greater/equal or less/equal to 0. This determines if it's faster to rotate clockwise or counter-clockwise towards the camera.
Now just like the positioning while loop we need to do the same for the rotation, but with two small changes. We need to keep looping until our current rotation is equal to 180 degrees. Currently we are facing our backs towards the camera and by turning 180 degrees we're facing the camera. So the while loop check will look like this.
Now just like we did before with our position we need to slowly rotate our player towards 180 degrees. To do that we make use of the "Slerp" function inside "Quaternion".
The slerp method works by providing it with two quaternion values and float value to adjust the rotation speed. The first quaternion value is the starting value and the second one is the target value. The float value determines the speed of how fast you rotate from your starting rotation to your target rotation.
So we basically provide the function with our current rotation value and then our target rotation, which is 180 degrees. We then adjust the speed by doing the same trick as we did with our positioning speed by doing "Time.deltaTime * positioningSpeedOnLevelNode".
In the end your function will look like this.
Now we need to do the same thing for the opposite direction. So what we can do is copy the entire if statement and paste it in again. In the end it will look something like this.
Now we have to reverse everything. So in the if statement the ">" to "<", in the while loop we need to add a "-" in front of the 180 and in the slerp function and we also need add the "-" in front of the 180. In the end the function will look like this.
We can now go back into Unity and press play and see our player rotate towards the camera at a reasonable speed.

Last updated
