Search Unity

How to create proper Enemy Ai

Discussion in 'Navigation' started by vickymotasara, Oct 12, 2020.

?

Are You interested in such game

  1. Yesssss :-)

    100.0%
  2. Nooo!!!! :- (

    0 vote(s)
    0.0%
  1. vickymotasara

    vickymotasara

    Joined:
    Jun 2, 2020
    Posts:
    6
    Currently I am Working On Game Based on A movie, In which player is stuck inside a maze and he has to find keys to unlock doors and solve sum puzzles and gt out of maze also he is followed my zombies. Every thing is ready accept Enemy. I want to Create An Enemy That moves Randomly in side the Maze , if Enemy Sees Player then it moves towards player and attack him or if Enemy Hears a gun Shot then it should Trace the aprrox. position of gun shot.


    This is My Enemy Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.Characters.ThirdPerson;
    5. using UnityEngine.AI;
    6. public class ScanForPlayer : MonoBehaviour
    7. {
    8.     public float viewDistance;
    9.     public float viewAngle;
    10.     public LayerMask viewMask;
    11.     Transform player;
    12.     bool playerIsVisible;
    13.     public NavMeshAgent agent;
    14.     public ThirdPersonCharacter character;
    15.     void Start()
    16.     {
    17.         player = GameObject.FindWithTag("Player").transform;
    18.         agent.updateRotation = false;
    19.     }
    20.     void Update()
    21.     {
    22.         playerIsVisible = CanSeePlayer ();
    23.         if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting)) {
    24.             AgentMover ();
    25.         }
    26.         character.Move (agent.desiredVelocity, false, false);
    27.     }
    28.     bool CanSeePlayer()
    29.     {
    30.         if (Vector3.Distance(transform.position, player.position) < viewDistance)
    31.         {
    32.             Vector3 dirToPlayer = (player.position - transform.position).normalized;
    33.             float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
    34.             if (angleBetweenGuardAndPlayer < viewAngle / 2f)
    35.             {
    36.                 if (!Physics.Linecast(transform.position, player.position, viewMask))
    37.                 {
    38.                     return true;
    39.                 }
    40.             }
    41.         }
    42.         return false;
    43.     }
    44.     Vector3 RandomNavSphere (Vector3 origin, float dist, int layermask)
    45.     {
    46.         Vector3 randDirection = Random.insideUnitSphere * dist;
    47.         randDirection += origin;
    48.         UnityEngine.AI.NavMeshHit navHit;
    49.         UnityEngine.AI.NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
    50.         if (Vector3.Distance (transform.position, FindObjectOfType<Shooting>().FirePos) < viewDistance/2 && FindObjectOfType<Shooting>().isShooting) {
    51.             return FindObjectOfType<Shooting> ().FirePos;
    52.         }
    53.         return navHit.position;
    54.     }
    55.     void AgentMover ()
    56.     {
    57.         if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting) || agent.isStoppeds) {
    58.          
    59.      
    60.             if (Vector3.Distance (transform.position, player.position) > 1) {
    61.                 if (playerIsVisible) {
    62.                     agent.SetDestination (player.position);
    63.                 } else {
    64.                     agent.SetDestination (RandomNavSphere (transform.position, viewDistance * 10, viewMask));
    65.                 }
    66.             } else {
    67.                 agent.isStopped = true;
    68.                 Debug.Log ("Dead");
    69.             }
    70.         }
    71.     }
    72. }
     
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    Why is your enemy script, which moves the enemy called "ScanForPlayer " ?
    It clearly does more than that.

    Seems like it should work - whats the exact thing you want, that the script doesn't do for you?
    Hearing things? Should be a messaging system or a spherecast I guess.
    Running to the position where the noise came from - you need something like a
    Vector3 inspectPosition
    .

    If you are asking about general AI - I suggest Behaviour Trees, I've written my own one with the experimental UI Elements GraphView and the modularity as well as runtime-debugging, makes it a charm to create comepelling AI.
    Other options are:
    - State Machines (simple to write but mostly all hardcoded, you can't really switch things in the editor)
    - GOAP (I also wrote a GOAP Ai but it was so unpredictable - no matter how I tweaked the values, it always managed to kill me while testing, also it led to "realistic" but boring gameplay in my shooter game)
    - Utility AI (tried this one too for a simulation game, it was also unpredictable and is usually used for "needs" like hunger, thirst, sleep, .. this one is better for SIMS like games -- can be combined with other AIs)
    - Machine Learning (I wasn't able to test this one yet because python and time .. but from what I've seen so far, this is the most unpredictable one and if you don't want it for a specific job no other AI can manage, I'd stay away from it in GameDev, as it will take way more time to train it, than to get the same result with any other AI)
    - OCAI (one class AI, I made the name up, but it's basically what you have right now, this one is OK for gamejams and prototyping, write everything in one script, using switch-case or if-else conditions for every possible behaviour. Not modular, not extendable, not maintainable - use at own risk)

    For all the AIs you can find more info online ^^