Practical Game AI Programming by Unknown

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



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.