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
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.
What's Done in Darkness by Kayla Perrin(26297)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18822)
Shot Through the Heart by Mercy Celeste(18701)
The Fifty Shades Trilogy & Grey by E L James(18621)
The Subtle Art of Not Giving a F*ck by Mark Manson(13932)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(13870)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(12853)
Scorched Earth by Nick Kyme(12523)
Drei Generationen auf dem Jakobsweg by Stein Pia(10754)
Suna by Ziefle Pia(10687)
Scythe by Neal Shusterman(10043)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9302)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9114)
This is Going to Hurt by Adam Kay(8765)
Dirty Filthy Fix: A Fixed Trilogy Novella by Laurelin Paige(7393)
How to Make Love to a Negro Without Getting Tired by Dany LaFerrière(6786)
He Loves Me...KNOT by RC Boldt(6672)
Unleashing the Power of UX Analytics: Proven techniques and strategies for uncovering user insights [Team-IRA] [True PDF] by Jeff Hendrickson(6453)
Interdimensional Brothel by F4U(6114)
