Page cover

Level node directions

Now that we have our player correctly positioned and rotated on our level node, it's time to set up our potential direction options for our level nodes.

Level node data

We first need to create a script for us that will hold the following information:

  • Direction options

  • Available directions

  • Previous path options

  • Our path connections

Later we'll add some more information to this script, but now this is all we need.

Now add the script to your level nodes and open the script. We can remove the "void Start" and "void update" functions since we don't need to use them here.

To create our direction options we are going to make use of enums, but we are going to adjust their values. Normally enums start at zero, but for our case we need to start them at one. So in our script the enum will look like this.

Now we need to create two lists from this enum, one will be used to represent all the available direction you can go in from that node and the other all the previous paths you can go to from that node.

Lastly we need a list of all the paths that are connected to that node. So in the end your script should look something like this.

In Unity you'll see the following.

Now what we can do is fill in this information for the two level nodes. Currently we have two level nodes and one path. This means both nodes only have one path connected, one available direction, but only one of them has a previous path option. You can see from this visual representation what I mean by that.

Now lets set up the information for our level one node. We know it only has one available path connection to the right, so we set our available connected path value to "RIGHT". We don't have a previous path connected to so we can leave this at zero. We do have one connected path, so we set that path in our connected paths list. In the end your level node should look something like this.

Note: A good thing to do is to name your path objects to the level node connections they represent. That way it makes it easier to figure out which path connects which two nodes.

The last one we need to setup is our level two node. We know it has one available path connection to the left, so we set our available connected path value to "LEFT". We have a previous path connected on this end to the left side, so we also set this value to "LEFT". Lastly, we need to set our connected path. There is currently only one path connected so we set that path in our connected paths list. In the end your level node should look something like this.

Selecting a direction

Now that we have our level nodes set up and ready, we can now start setting up a way for the player to choose which direction they want to go and if that chosen direction is available to them.

First, we need a way of storing the level node information the player is currently standing on. We do this by adding a serialized reference to this inside our path navigator script.

Now we need to set this value and to do this we can simply add "currentlevel = other.GetComponent<LevelNodeData>();" inside our on trigger enter function.

And that helps us get the information we need to check for which direction the player is allowed to go to. Now lets start making a way to check which direction wants to go in and if this option is available.

To check this we create a new function called "CheckDirection();" that takes a vector2 parameter named "selectedDirection".

Now we go back to our "OnDirectionInput" function and add this function underneath where we set our "directionInput" value. So in the end your function should look like this.

Now that has been set up we can go back to our function we've just created.

The first thing we need to check if our Y or X value of "selectedDirection" is either equal to one or minus one. To make it a bit easier to figure out which if statement represents which direction, I've added comments above them saying if they are either up, down, left or right. The function should now look like this.

What we need to do now is add a for each loop in all our direction checks. To do this we need to loop over all are available directions we've set in our level node data script. So in each if or else if statement will be adding "foreach (LevelNodeData.Direction i in currentLevel.allAvailableDirectionOptions)". The function will now look like this.

Up next we need to check inside our if statement if our "i" variable is equal to our set enum values. So that will be either up, down, left or right. The function will now look like this.

Now the rest of the code will be exactly the same in all of the if statements beyond this point, except for one line. Once we reach that point I'll let you know, but for now let's just use our up direction as an example for the rest of the directions.

We need to store at what index the up direction is stored in our available directions enum list. What I mean by that is we need to get the position of our up direction in our list. So if we first have down and then up inside our list, this means up is at the second position of the list.

So in order to store this information we need to create a temporarily integer that stores the index value. To get this value we need to make use of Unity's "IndexOf" function to get index value we want from our enum list. The function will look like this.

The next integer we need to store is our enum direction value. This is the point that will be different from the other directions. In this example we use "UP" as the enum direction value we want, but for the other directions you need to use their corresponding enum direction values.

Now we create a check to see if our current path is not equal to the path we're getting from our connected paths lists index value. This means we are not continuing on the same path anymore, but on another path connection to another level node.

Now if this is the case we need to set that path to our current path value.

Now we need to check if our direction we've chosen is part of the previous directions list option. To do this we need to create a for each loop to loop over all the available previous path direction options in that list.

Now we need to check if our "j" value is equal to the value we get from our direction value integer. If we would to check this we would get an error saying we can check an enum with an integer. To fix this we need to cast our "j" value as an integer.

Now if this is true, it means we are going backwards on the path. That means we need to set our progress value to one before we start our "MovePlayer" coroutine. If we are going forward, we can just start our "MovePlayer" coroutine.

Now we have everything ready for when the player isn't on the same path anymore, but what if the player still is trying to move back and forth between level nodes on the same path?

To do this we need create another if statement underneath our current path not equal statement checking if our previous direction options list is not zero. Here we are trying to check if we can go backwards on our current path.

Now what we do is basically copy and paste over our for each loop we've inside our previous if statement check and paste it inside our previous direction count if statement check, but remove the else statement. The reason for this is because we have to do the exact same check and movement configurations in order to move backwards on the curve between the two level nodes only when a previous path option is available to us.

The last thing we need to add is an else result when the previous direction options list is equal to zero, meaning we are trying to go backwards on a new path, but the starting point of the path is at the level node you're currently standing on.

Now we can move to other level nodes back and forth, but there is a catch. If we keep pressing to go up now we can keep increasing our movement speed towards the next level node because we never check if we're already moving or not.

To prevent this from happening, we create a public boolean function that has a "Get" and "Set" property. This allows us to either get the value of our boolean or set the value of our boolean.

For this tutorial I'll be making a boolean with the name "canMove", but you can name yours to whatever name you see fit.

We need to set our can move boolean to true in our "void Start" so when we press play in Unity we can start moving around on the world map. Later this gets removed because of a future implementation, but for this stage we need it.

Now we have our boolean set up, we can go back to our "CheckDirection" function to add a few lines to help us prevent rapid firing our movement.

First, we need to set our can move boolean to false when our direction input is equal to "UP".

Underneath where we set our current path to be equal to the index of our connect paths list, we need to create an if statement checks if our current path is unlocked or not. Only when it is true, we want the player to move towards the level node. We need to wrap this if statement around the for each loop.

If this is not true, we need to reset our can move boolean back to true.

Now we need to go inside our previous path options to check if the path we want to go on is already unlocked or not before we set our progress to one and start our "MovePlayer" coroutine. When the result is false we need to set our can move boolean back to true.

The last else statement we also need to add in an extra if statement check for it the path is unlocked or not. When it is we need to start our "MovePlayer" coroutine, else we need to set our can move boolean back to true.

Now we can copy everything inside our if statement that checks if it's equal to "UP" and paste it inside the other equal if statement direction checks.

IMPORTANT: Don't forget to change your direction value result of your enum to be the same as the if statement to check if your result was equal to a specific direction.

Now we're done with the "CheckDirection" function, but we still need to reset our can move boolean and progress float.

To do this we go over to our "PositionPlayerOnLevel" function we've created previously and underneath the final if statement add "yield return new WaitForSeconds(0.5f);" This makes the code pause for half a second before continuing on, giving us a sort of cool down buffer before we can move again to the next level node. We also need to reset our float progress back to zero and or can move boolean to true.

The last thing we need to do is go into our "OnDirectionInput" function where we control our direction input and wrap our "CheckDirection(directionInput);" function in an if statement to check if the can move boolean is true or not. Only when it's true it should fire of the function.

Now let's go back to Unity and test this out. Go to your path object, click on it and check mark the "Unlocked" boolean. This will now allow us to move across the path.

The end result should look something like this.

With that done, you've now fully set up your multiple path movement across different level nodes. The next step is to visualize our path by decorating it.

Last updated