Practical Game AI Programming by Unknown
Author:Unknown
Language: eng
Format: epub
Published: 0101-01-01T00:00:00+00:00
As we can see, straight lines do not fit the genre that we are currently creating. It works perfectly in other genres, like Tower Defense game, but for a racing game it is necessary to redefine the code to adjust to the situation that we are creating.
The rest of the code is exactly that, adjustments for the situation that we are creating, which is a car moving on a race track. There are force elements such as drag, which is the friction between the car and the road, represented in this code. When we turn the car, it will slide according to the velocity of the car at that moment, and these details are taken into consideration here, creating a more realistic point to point movement where we can see that the car is reacting according to physics.
This is the full code that we have used in our example:
public static bool raceStarted = false; public float aiSpeed = 10.0f; public float aiTurnSpeed = 2.0f; public float resetAISpeed = 0.0f; public float resetAITurnSpeed = 0.0f; public GameObject waypointController; public List<Transform> waypoints; public int currentWaypoint = 0; public float currentSpeed; public Vector3 currentWaypointPosition; void Start () { GetWaypoints(); resetAISpeed = aiSpeed; resetAITurnSpeed = aiTurnSpeed; } void Update () { if(raceStarted) { MoveTowardWaypoints(); } } void GetWaypoints() { Transform[] potentialWaypoints = waypointController.GetComponentsInChildren<Transform>(); waypoints = new List<Transform>(); foreach(Transform potentialWaypoint in potentialWaypoints) { if(potentialWaypoint != waypointController.transform) { waypoints.Add(potentialWaypoint); } } } void MoveTowardWaypoints() { float currentWaypointX = waypoints[currentWaypoint].position.x; float currentWaypointY = transform.position.y; float currentWaypointZ = waypoints[currentWaypoint].position.z; Vector3 relativeWaypointPosition = transform. InverseTransformPoint (new Vector3(currentWaypointX, currentWaypointY, currentWaypointZ)); currentWaypointPosition = new Vector3(currentWaypointX, currentWaypointY, currentWaypointZ); Quaternion toRotation = Quaternion. LookRotation(currentWaypointPosition - transform.position); transform.rotation = Quaternion.RotateTowards (transform.rotation, toRotation, aiTurnSpeed); GetComponent<Rigidbody>().AddRelativeForce(0, 0, aiSpeed); if(relativeWaypointPosition.sqrMagnitude < 15.0f) { currentWaypoint++; if(currentWaypoint >= waypoints.Count) { currentWaypoint = 0; } } currentSpeed = Mathf.Abs(transform. InverseTransformDirection (GetComponent<Rigidbody>().velocity).z); float maxAngularDrag = 2.5f; float currentAngularDrag = 1.0f; float aDragLerpTime = currentSpeed * 0.1f; float maxDrag = 1.0f; float currentDrag = 3.5f; float dragLerpTime = currentSpeed * 0.1f; float myAngularDrag = Mathf.Lerp(currentAngularDrag, maxAngularDrag, aDragLerpTime); float myDrag = Mathf.Lerp(currentDrag, maxDrag, dragLerpTime); GetComponent<Rigidbody>().angularDrag = myAngularDrag; GetComponent<Rigidbody>().drag = myDrag; }
If we start the game and test it, we can see that he is working fine. The car drives by itself, turns smoothly, and completes the track as intended.
Now that we have the basic point to point movement completed, we could implement more functions for the AI driver and start developing the game as we want. It is always recommended to start with the main functions of the gameplay before developing any details. This will help identify any ideas that we had planned for the game that do not work as well as we thought.
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.
Exploring Deepfakes by Bryan Lyon and Matt Tora(6696)
Robo-Advisor with Python by Aki Ranin(6626)
Offensive Shellcode from Scratch by Rishalin Pillay(5542)
Ego Is the Enemy by Ryan Holiday(4670)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4326)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(4018)
Python for ArcGIS Pro by Silas Toms Bill Parker(3638)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3341)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3160)
Speed Up Your Python with Rust by Maxwell Flitton(3115)
Liar's Poker by Michael Lewis(3090)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3089)
Extreme DAX by Michiel Rozema & Henk Vlootman(3061)
Agile Security Operations by Hinne Hettema(3023)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3010)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3010)
Essential Cryptography for JavaScript Developers by Alessandro Segala(2981)
Cryptography Algorithms by Massimo Bertaccini(2892)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2877)
