Sunday, 29 November 2015

AI in Unity with explanation

AI (short for artificial intelligence) is incredibly important to have in games, as it gives life to the world you've created and adds massive amounts off depth to it. This can range from an enemy being able navigate a level and jump up platforms in order to catch and kill the main character (with a RigidBody component attached in order to simulate Unity's built-in gravity), to NPCs following the player and avoiding ranger as they guide them to safety, one way or another AI is usually implemented into a video game. It even dates back to the beginning of games, as talked about by gaming journalist (Grayson. N, 2015), all of the ghosts in the original over 30 year-old Pac-Man game had their own different 'personalities' and movement patterns for catching the player.

The red ghost would chase them down from behind following Pac-Man throughout the maze, blue would base his actions on the red one as he moved to areas that would corner Pac-Man as he was being chased by red, next-up is the pink ghost who is the smartest (with her trying to think ahead and predict where the player might be), and lastly is orange who is designed to do their own thing wander about, and sometimes choose to not even attack the player, in order to really make these enemy ghosts seem more like actual characters that can shine through the game's retro graphics than just being all the same.

Pac-Man Ghosts Are Smarter Than You Think
All four Pac-Man Ghosts from the original game, where each had their own strategies of chasing the player. (Grayson. N, 2015)
Now we have some examples let's go back to AI in Unity! For example a common type of AI used is Fuzzy Logic. Which is, (as supported by (WhatIs, 2015)logic that identifies and works out more than just basic calculations, such as if something is true or false. With fuzzy logic statements and opinions can be shown to have either have a chance of either being correct or incorrect or happening or not happening. For example, saying ‘it may rain today’, could be 100% true if there are thick dark clouds directly above you with thunder roaring in the distance, 70% true if there are a lot of clouds around, 50% true if it's foggy with a few clouds and 0% true if it’s bright and sunny without a cloud in the sky. Fuzzy logic is very useful when used in game engines, especially with artificial intelligence. As NPC enemies can use it work out where the player will go or allies can calculate whether or not they should heal you or wait until you’re more injured. Here's an extract of code for making decisions when giving chase, taken from a an enemy AI script (from the official (Unity3d.com, 2015) page) from that depicts a patrolling enemy that wanders about in a set direction, and chases the player if they're spotted, but if the player leaves their line of sight for a certain amount of time, then they'll reset their position and start patrolling again. This is the decision making process:

' void Chasing ()
    {
        // Create a vector from the enemy to the last sighting of the player.
        Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
        
        // If the the last personal sighting of the player is not close...
        if(sightingDeltaPos.sqrMagnitude > 4f)
            // ... set the destination for the NavMeshAgent to the last personal sighting of the player.
            nav.destination = enemySight.personalLastSighting;
        
        // Set the appropriate speed for the NavMeshAgent.
        nav.speed = chaseSpeed;
        
        // If near the last personal sighting...
        if(nav.remainingDistance < nav.stoppingDistance)
        {
            // ... increment the timer.
            chaseTimer += Time.deltaTime;
            
            // If the timer exceeds the wait time...
            if(chaseTimer >= chaseWaitTime)
            {
                // ... reset last global sighting, the last personal sighting and the timer.
                lastPlayerSighting.position = lastPlayerSighting.resetPosition;
                enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
                chaseTimer = 0f;
            } 
        }

Next I will talk about behaviours and neural nets. Behaviours, as sourced from (Simpson.C, 2014)Behaviours is essentially how AI works. It is sorted through behaviour trees (a high-end graphic modelling language) to model and represent the AI in games. For example different in- game entities in Unity can have different behaviours depending on the situation, such as being idle, patrol, follow, flee, seek, attack, defend, hurt, change, heal, help and dead. A behaviour tree is a tier list that lists nodes in order of importance that decide how an AI entity (such as an NPC) make decisions. The very bottom of the tree (symbolising the leaves) are the actual commands that decide what the NPC does and the branches are different types of nodes that determine how the tree’s AI walks down the tree’s branches to reach the commands of which is best suited to what is happening in-game. An example of a behaviour tree is below.
An example behaviour tree represented how the decisions are filtered down to different 'leaves'. (Simpson. C, 2014)

Now onto neutral nets! Which is more of a theory than an implementation of AI. Once again, with information from (TutorialsPoint b, 2015)Neural nets is the theory of AI almost being human,  especially in the future. Where AI will almost become  human such as copying the pulses in the brain and how  it uses these pulses to tell our bodies commands. Nobody knows exactly what it is, however it tries to simulate how the brain works and emulate the human mind using very advanced technology that learns new things that it experiences throughout its time.

No comments:

Post a Comment