Unity 4.x Game AI Programming by unknow

Unity 4.x Game AI Programming by unknow

Author:unknow
Language: eng
Format: epub
Publisher: Packt Publishing


We set the parent of the object of our boid as origin, meaning that this will be the controller object to follow generally. Then, we grab all the other boids in the group and store them in our own variables for later references.

The StartCoroutine method starts the UpdateRandom() method as a coroutine:

IEnumerator UpdateRandom() { while (true) { randomPush = Random.insideUnitSphere * randomForce; yield return new WaitForSeconds(randomFreq + Random.Range(-randomFreq / 2.0f, randomFreq / 2.0f)); } }

The UpdateRandom() method updates the randomPush value throughout the game with an interval based on randomFreq. Random.insideUnitSphere returns a Vector3 object with random x, y, and z values within a sphere with a radius of the randomForce value. Then, we wait for a certain random amount of time before resuming the while(true) loop to update the randomPush value again.

Now, here's our boid behavior's Update() method that helps our boid entity comply with the three rules of the flocking algorithm:

void Update () { //Internal variables float speed = velocity.magnitude; Vector3 avgVelocity = Vector3.zero; Vector3 avgPosition = Vector3.zero; float count = 0; float f = 0.0f; float d = 0.0f; Vector3 myPosition = transformComponent.position; Vector3 forceV; Vector3 toAvg; Vector3 wantedVel; for (int i = 0;i<objects.Length;i++){ Transform transform= objects[i]; if (transform != transformComponent) { Vector3 otherPosition = transform.position; // Average position to calculate cohesion avgPosition += otherPosition; count++; //Directional vector from other flock to this flock forceV = myPosition - otherPosition; //Magnitude of that directional vector(Length) d= forceV.magnitude; //Add push value if the magnitude, the length of the //vector, is less than followRadius to the leader if (d < followRadius) { //calculate the velocity, the speed of the object, based //on the avoidance distance between flocks if the //current magnitude is less than the specified //avoidance radius if (d < avoidanceRadius) { f = 1.0f - (d / avoidanceRadius); if (d > 0) avgVelocity += (forceV / d) * f * avoidanceForce; } //just keep the current distance with the leader f = d / followRadius; UnityFlock otherSealgull = otherFlocks[i]; //we normalize the otherSealgull velocity vector to get //the direction of movement, then we set a new velocity avgVelocity += otherSealgull.normalizedVelocity * f * followVelocity; } } }



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.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56733)
What's Done in Darkness by Kayla Perrin(26765)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20391)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20207)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20190)
The Fifty Shades Trilogy & Grey by E L James(19232)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19219)
Shot Through the Heart by Mercy Celeste(19079)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17259)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17197)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17040)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16960)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16589)
The Subtle Art of Not Giving a F*ck by Mark Manson(14550)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14282)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13930)
Scorched Earth by Nick Kyme(12917)
Drei Generationen auf dem Jakobsweg by Stein Pia(11115)
Suna by Ziefle Pia(11039)
Scythe by Neal Shusterman(10506)