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
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.
API Testing and Development with Postman by Dave Westerveld(3636)
Learning C# by Developing Games with Unity 2020 by Harrison Ferrone(2630)
Software Architecture for Busy Developers by Stéphane Eyskens(2336)
2021 Beginners Guide to Python Programming Language: A Crash Course to Mastering Python in One Hour by Elmer Gary & Elmer Gary(1884)
Machine Learning for Algorithmic Trading by Stefan Jansen(1631)
Hands-On ROS for Robotics Programming by Bernardo Ronquillo Japón(1576)
Delphi GUI Programming with FireMonkey by Andrea Magni(1457)
Game Development Projects with Unreal Engine by Hammad Fozi & Goncalo Marques & David Pereira & Devin Sherry(1402)
Cloud Native with Kubernetes by Alexander Raul(1376)
Datadog Cloud Monitoring Quick Start Guide by Thomas Kurian Theakanath(1347)
Software Architecture Patterns for Serverless Systems by John Gilbert(1339)
Practical Node-RED Programming by Taiji Hagino(1336)
Automate It with Zapier by Kelly Goss(1318)
Practical System Programming for Rust Developers by Prabhu Eshwarla(1312)
Delphi Programming Projects by William Duarte(1296)
Mastering React Test-Driven Development by Daniel Irvine(1290)
Developing Multi-Platform Apps with Visual Studio Code by Ovais Mehboob Ahmed Khan & Khusro Habib & Chris Dias(1253)
Ghidra Software Reverse Engineering for Beginners by A. P. David(1245)
Learn Spring for Android Application Development by S. M. Mohi Us Sunnat(1236)
