Bird AI and Inheritance


Birds that aren’t real.

Introduction


The purpose of this backlog task was to create an AI that would represent birds fleeing from the player. I broke the task up into two parts. The first was handling the collision of the player, setting a trigger volume and having the script recognize that the object colliding with it is in fact a player and not some NPC or animal. The second task was having the bird determine where the player was in relation to its own position and fly in the opposite direction at an upwards angle. When making this script I had it inherit from another team members Base AI class. This means I had to familiarize myself with state machines and inheritance as I'd never worked so closely with them before.


Goals

  • Familiarize self with the Base AI script.
  •  Create a script that recognizes player collision within a trigger volume without blocking the players movement.
  • Understand the logic behind the AI's movement calculation.
  • Add to the script that determines where the player is in reference to the GameObject and then fly in a direction opposite to it.


Personnel


Primary – Ryley Delaney

Secondary – Keanu Brown-Duthie


Technologies, Tools, and Resources used


Provide resource information for team members who wish to learn additional information from this spike.
Collider OnTriggerEnter Documentation: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
gameObject.CompareTag documentation: https://docs.unity3d.com/ScriptReference/GameObject.CompareTag.html
Collision with only detection: https://answers.unity.com/questions/944611/how-do-i-pass-through-certain-collide...
Vector 3: Move Towards: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html                                                                    Online Web Whiteboard: www.webwhiteboard.com


Tasks undertaken

Firstly I familiarized myself with the Trigger and Collision documentation to understand which would be best suited for the task. Creating a temporary scene with a base cube and trigger volume to test goal 2. I spent a good amount of time reading through the Base AI commentation and functionality to understand how It would be used for the purpose of my task. At this point it was time to create the functionality for detecting the players location and creating a marker for the "bird" to fly towards.

Because the work for this is done in a 3D space. I thought it would be helpful to visualize the problem and focus on the steps taken to find "the opposite" point in space. After erasing the entire board and writing for around 30 minutes this was the final position of the board.


In this section I'm going to discuss the logic behind the bird's location capabilities so feel free to skip to the next section if that doesn't interest you. I found that the easiest way to visualize the state of the game world was with a bird's eye view (pun not intended) of the 3 planes. In unity these planes are the X and Z planes as opposed to X and Y. Each individual bird needs to fly away from the player in a different direction. So it wouldn't be a collective point but rather calculated the moment the player enters the volume trigger.

Eventually I determined the three steps that need to be taken to determine where the flight direction (that is the point opposite the player on the X and Z axis) is. The first thing is to determine the players X and Z position within the game world. This is done on collision using a playerPosition Vector3 variable. Next we need to subtract the bird's position by the player position to determine the difference. Then subtract that number from the bird's position to give us our flight direction.

For example:
If the player is at a position ( 4x and -7z) when it collides with the trigger volume.
and our bird is at position (10x and -3z) on the board.

(10 - 4x , -3 + 7z) = (6x, 4z)
(10 + 6x, -3 + 4z) = (16x, 1z)

Which is the opposite position. Our bird then takes this location and flies towards it while increasing in the Y coordinate by about 7.

Where to now?

The plan from here onward is to create a networked system where instead of each individual bird flying off. When one bird is alerted, it will send a notification to all other birds within its trigger volume that they should also flee and so on. This will involve some sort of communication system. Alternatively the birds may return to their original location after some time passes assuming there is no player occupying its original space.

Leave a comment

Log in with itch.io to leave a comment.