Beginning Game AI with Unity by Sebastiano M. Cossu

Beginning Game AI with Unity by Sebastiano M. Cossu

Author:Sebastiano M. Cossu
Language: eng
Format: epub
ISBN: 9781484263556
Publisher: Apress


At line 1, we define the matrix that we will use to store all our waypoints. This matrix will represent the space as a tiled board in a board game, as we said in the previous section.

At line 2, we declare a list of nodes that we will use to represent the final path that the agent will walk by reaching every waypoint/node in the list. The list will be traversed using the counter defined at line 3.

At lines 5–7, we declare public fields that will contain our Waypoint prefab and the different materials representing the goal and the nonwalkable nodes, while at line 14 we define an integer variable that we will use as an offset to put some space between the points in the 3D scene. For each node, we will create an instance of the Waypoint prefab so that the node can be represented visually in the 3D space.

At lines 9–12, we declare some of the variables that we already saw in the previous section. Those variables are goal, speed, accuracy, and rotSpeed, and they will be useful to implement the actual movement of the agent in the 3D space. The principle of how the agent will move toward an objective point will be the same, but the logic will be a bit different as we have a list of points that make the path and not just a single node. The goal will be set the first time to the first node of the path list, and it will change to the next one in the list when the agent will reach it.

Finally, at lines 16 and 17, we define two containers in which we will put the references to the starting and final nodes.

OK, we finished declaring variables; now we can concentrate on the actual functionality by implementing some methods.

As we said, for every node, we will need the list of its adjacent nodes and store it in the neighbors property. So let’s write a method to calculate this list. We assume that we will receive a reference to the matrix containing all the nodes and the coordinates to the matrix for the current node of which we want to calculate the adjacent nodes.

The adjacent nodes will be calculated only considering the four basic directions: up, down, left, right – this means that we will have at most four adjacent nodes.

To obtain the adjacent nodes in a 2D matrix in the way I just explained, we will need to add or subtract 1 to the row and column numbers. For example, given a matrix called M, we can get the neighbors of an element at coordinates M[r,c] (where r is the row number and c is the column number) like this:Up: M[r-1, c]

Right: M[r, c+1]

Down: M[r+1, c]

Left: M[r, c-1]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.