In this section, you will create a Waypoint script that you will use to define positions in your world for your enemies to move between.
It is possible to create a Prefab directly in the Project explorer.
If all went well, you should be in the Scenes | Prefab editor with an Empty Game Object named "Waypoint":
To make it easy to know where the Waypoint is when you add it to a Scene, you can add a 3D Sphere (or another shape if you prefer).
This will add a child object to the Waypoint called Sphere that is positioned at the center of the Waypoint. You can verify this by ensuring the Transform's position is (0, 0, 0)
You may have also noticed that the defaults Sphere generated comes with 3 additional components that are visible in the Inspector.
Because the Waypoint does not need to collide with other objects, you can safely remove this component.
Another way you can exit the Prefab editor is by clicking the Scenes text that appears in the top left of the Scene view.
You can add a Prefab directly into your Scene by dragging it into the Scene view.
Once again, you might find it difficult to precisely place the objects in the Scene view. To help with placement, you can enable Grid Snapping.
Clicking this button will show the Grid and Snap settings at the top of the Scene View
This menu allows you to specify the size of the Grid and allows you to turn on Snapping.
0.5
With Grid Snapping enabled, you should now be able to place your waypoints more precisely on your tile map using the Move Tool.
If you're having trouble selecting your Waypoints, you can disable the ability to click your Tile Grid elements by hovering to the left of the Grid Game Object in the Hierarchy and clicking the finger icon.
This will disable your ability to click on that Game Object in the Scene View. You can re-enable interactions by clicking the finger icon again.
Currently, your Waypoints are visible in your Game View which means your player will see them during game play. You can test this by Entering Play Mode.
One way you could hide the Waypoints would be to disable the Mesh Renderer. (Don't do this)
However, it is very convenient to be able to see the Waypoints in the Scene view so we can adjust them.
Another option is to tell our Main Camera to ignore rendering them. This can be done by setting adding the Waypoints to a special rendering layer and then updating the Main Camera's Culling Mask to ignore that layer.
In the Inspector you can see and modify the layer of a Game Object.
Culling refers to the process of determining which objects in a scene are not visible to the camera and excluding them from rendering to improve performance. Next, you will update the Main Camera's Culling Mask to exclude the Waypoint layer.
By default, the camera's Culling Mask will render everything.
You can verify that the camera is no longer rendering your Waypoints by entering the Game View.
With your Waypoints placed on your map, it is now time to make them aware of their order. You can accomplish this by creating a Waypoint component that knows which Waypoint comes next in the sequence.
private set
[field: SerializeField]
attributeCan you remember how to add your new component to your Waypoint prefab? If you're having trouble, you can review how you added the EnemyMovement component in 06 - Adding an Enemy
By creating a Next property that has the type Waypoint , you have made it possible to specify the Next property of each Waypoint in the Inspector.
Note: If you click the object in the Next field, it will highlight in the Hierarchy. If you double-click the object in the Next field, it will select that object.
You can also set the an object property in the Inspector by dragging a game object into the property field.
Sometimes this can be challenging to do because you can accidentally click the object you're trying to drag, changing the selected object in the Inspector. To help with this, you can open a new inspector window for a specific game object by Right Clicking on it and select Properties.
Test out each of the methods for setting the Next for each of your Waypoints property to figure out which one works best for you. You should leave the final Waypoint's Next property as None. You will use this to signal to the Enemy that it has reached the end of the path.
Depending on the number of Waypoints you have created, your Hierarchy might be feeling a bit crowded. To help with this, create an Empty Parent that contains all of the Waypoints as children object.
When you're finished, your Hierarchy should look similar to the image below:
While setting each of the Next properties, you may have sensed that this process could be error prone. That is, it is easy to set the wrong Waypoint. To help with this, you can use the OnDrawGizmos and OnDrawGizmosSelected methods to draw additional information in the Scene View.
The OnDrawGizmosSelect method is used to draw information in the Scene View when a Game Object is selected.
using UnityEditor;
to your using
statements
With your new gizmo in place, you can now more easily verify that your Waypoints are connected properly:
You most likely encountered NullReferenceException when you selected your final Waypoint. This is because the Next property is set to None. For class objects, this is the equivalent to a null
value. To prevent this, you should add a null check to your code that exists your method early if Next == null
.
If you prefer, you can also use the OnDrawGizmos method which is executed in the Scene View even when the Game Object is not selected.
In the Scene View verify your path is correct
It is important to review what you've learned in this lesson and apply it in a new context. Before continuing, I encourage you to attempt the Binary Waypoint Challenge in which you will be challenged to create a new script to accommodate splitting paths.
With your Waypoints set up, it is now time to update your Enemy Movement script to move between each Waypoint.