Sunday, August 23, 2015

Basic AI: Part 2

The AI we developed in the last post has a few limitations. If had the chance to play around with it you saw that the bots have a tendency to float when you jump. Now this isn't necessarily bad depending on the type of bot you want to make. For example you could make a small quadcopter drone or some type o hovering drone. But if we want real humanoids and animals we need to fix that problem.
The second limitation is that the moment you start the bots begin chasing you. In a real game this isnt bad depending on the context. For example say your character reaches a reputation of negative 45. Now we want to spawn a bounty hunter to dispatch the character. We dont want the spawn point to be close but not really far. We want to add the illusion on long distance travel. So we would create the point close to the player and spawn the bot and have it immediately seek out the player.

For this tutorial we will address these two points. You will need the past code for this tutorial, the link for it is here: Chase AI

In Unity the up/down variable is 'y'.  This will be used for our gravity. What we will do is check if the bot is grounded, if its not, we will take the my_Velocity Vector variable and adjust the 'y' value of it and set it to zero.

      my_Velocity.y = 0;

Now we will set some conditionals for when the bots will chase you. We will have two triggers, one will be timed. After a few seconds or minutes the bots will start. The other will be an invisible collider, when the player walks through it will begin chasing the player.

Triggered Chase:

First off  add a Sphere object to the game world and remove the Mesh Renderer component, then rename it to SphereTrigger, link the Sphere object to the bot (Note i call the bots Cylinders after the object). Next we need to make a new script that will be placed in the SphereTrigger. Name it 'ChaseTrigger', this script when finished and compiled will be attached to you Sphere object. Now place the Sphere object in front of your player character or somewhere in the scene.


Next make a new script and name it 'ChaseTrigger'. This script is straight forward, it will use an intrinsic function, a conditional and a boolean. This script will be accessed by the main AI script we will make. 

What we are doing here is using OnTriggerEnter(). This function is used on objects that serve as triggers for other events or actions. Within that function we use an if statement that checks if the object that collided with the OnTriggerEnter() is tagged as "Player"
(collision.gameObject.tag == "Player"). 
If so the the boolean value is set to true: collisionTrigger = true;

Next, copy the Basic AI script and make a new script, paste the script there. Make sure that the name of the script matches the name in the script. Next we need to make two variables one a game Object the other a script object.

The two new variables are the GameObject SphereColliderTrigger; this game object will do two things, one, it will be the placeholder for the sphere collider you place in the game earlier. Two, access the variables from the next variable which is an instantiate of the ChaseTrigger script you made: ChaseTrigger sphereTrigger.
Next we have to initialize the sphereTrigger script with the values by holding the values in the SphereColliderTriggerObject, this has to be done only once so place it in the Start() function.

Now for the Update(), it will just have a conditional.

if(sphereTrigger.collisionTrigger), is the main conditional. The initialized script from prior is calling the variable in its self: collisionTrigger to see if it has been set to true. The rest of the code is the same. 
Finally make sure to set all the fields of the scripts.

Press play and go.

Timed Chase:

This version is by far the fastest. All we need are two variables, a time to detonate: public float timeToBeginChase and a timer to be incremented till it reaches the threshold to detonate: public float timer. Copy the original Basic Basic AI script and add the following two variables in the heading.

Now in the Update() we need to update the timer variable and compare it to its detonation threshold.

The line: timer +=  1 * Timer.deltaTime; increments the variable timer each frame. The variable is later compared in the line if(timer >= timeToBeginChase) if the timer value is greater than or equal to 5, then the bot will be triggered. Now compile, and attach the script to a bot. As always remember to set the fields of the script like above. The code for all three scripts is below.

Chase Triggered, and timed

Other methods of AI include gauging the distance between a bot and player, making judgment calls, "sight" and "sound". Maybe ill try and make a few for examples.

No comments:

Post a Comment