Saturday, 23 January 2016

Coding - Alerts, Chase, CCand Search - John Howard

Task

To create an AI setup that allows it to Chase and Search for the player

Step One -  Core State Machine

Before even thinking about setting up the functions needed for chasing and searching I first need to establish a method of calling them. Thinking back on my previous project I realized the appropriate way of controlling the calling of functions would be the state machine. I actually really like using these as they help isolate code segments and therefore isolate problems, if they may arise. Simply they help structure the code into manageable areas and allow the calling of them through states. Within this state machine I set up three main states, NoAlert, Amber, and Alert. And three transitional states, TransNoAlert, TransAmber, and TransAlert. The transitional states allow me to call and stop the appropriate sections of other code before moving into the core state. Setting up this state machine didn't actually take much time or research due to my experience with them in the previous game.
Core Switch function

Step Two - Alert State Machine

Next I had to set up a state machine that would control what the AI would do when alert. This state machine is called when the AI has sight of the player. The two states within here would be Combat and Chase. For now I will leave combat and start working on that after I have finished the intial movement codes. This is because I want to have everything else sorted so that I can devote my full attention to creating a combat script. This statemachine has the same switch mechanic as the core state with a change of what controls the state. Basically when this state is active i'm constantly calling a distance checker which keeps track of the distance between the player and this gameobject(the AI). Then by using an if statement I can compare the distance against a float of combat distance and say that if the distance is less than or equal to the combat distance to change to combat state. This just means that the ai will stop when close to the player and start chasing again when not.
Distance checker and playeractive

Sidenote PlayerActive

I was finding it tiresome to constantly having to create a gameobject to assign the player too or calling the player via a Find command as it was both repetative and messy. So I created a script called PlayerActive that stored public statics enabling me to call any static from there in any script. So I stored variables such as the player gameobject and rigidbody.

Step Three - Chase

Setting up the chase was actually really simple. A constant calling of the ChaseUpdate() function constantly setting the nav.destination to the transform of the player in question.
Chase Script

Step Four - Search

Creating the search was the hardest part of the setup of these so far. But nonetheless I was able to create a rough working search function. Firstly the search script launches from the core state machine once the AI loses sight of the player after having chased or been combat with them. Within the sight script I have stored a variable of personal lastsight for the AI and the nav.destination is set to this point in space. Once the AI has reached this point a random point is created in a sphere around the AI and a timer is started. Once the timer reaches a certain value the core state is told to return the AI to patrolling. Now while this works I find it to be both very buggy and very hacky. I think turning this into a corotuine would be a much better idea and instead of basing the point on a sphere basing it in a cone that is roughly aimed at the player. This would allow the AI to search in a much more sensible manner. However as a rough setup this works.
This video showcases the search along with a tester squad function that I decided to remove for now.

Thoughts

 I would like to get this put into a game environment as soon as possible as it is important to test these mechanics. However we seem to have no player controller yet so I must continue testing using the standard one I have set up. Hopefully within the next week or so the player controller will be at a point that can be tested with the AI. 


No comments:

Post a Comment