Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Enemy AI Doesn't Work

Discussion in 'Scripting' started by Corrothon, Aug 16, 2020.

  1. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    I'm making a horror game but I have a problem with the AI, It doesn't work! When The Player is near it start glitching but when it's at spawn it starts shaking. Any Help? Here's 3 screenshot's:

    Edit: Here's my code (I Changed a little bit)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyAI : MonoBehaviour
    {
    Transform playerTransform;
    UnityEngine.AI.NavMeshAgent myNavmesh;
    public float checkRate = 0.01f;
    float nextCheck;





    // Start is called before the first frame update
    void Start()
    {
    if(GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
    playerTransform = GameObject.FindGameObjectWithTag("Player").transform;


    myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
    if(Time.time > nextCheck)
    nextCheck = Time.time + checkRate;
    FollowPlayer();
    }

    void FollowPlayer()
    {
    myNavmesh.transform.LookAt(playerTransform);
    myNavmesh.destination = playerTransform.position;
    }

    }
     

    Attached Files:

  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You are looking at the player so I guess you should do move forward as well.
     
  3. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Check rate is much to high use 1s here or even higher. Your if statement was wrong ..
    You can call CancelInvoke() if the agent should stop hunting.

    Here's some working code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyAI : MonoBehaviour
    7. {
    8.     Transform player;
    9.     NavMeshAgent agent;
    10.     public float checkRate = 1f;
    11.     float nextCheck;
    12.  
    13.     void Start()
    14.     {
    15.         player = GameObject.FindGameObjectWithTag("Player").transform;
    16.         agent = gameObject.GetComponent<NavMeshAgent>();
    17.         agent.updateRotation = true; // rotate the agent always to the player
    18.         agent.stoppingDistance = agent.radius * 2f; // we do this so the agent dont move the player away
    19.         InvokeRepeating("FollowPlayer", checkRate, checkRate); // follows the player again every checkRate seconds
    20.     }
    21.  
    22.     void FollowPlayer()
    23.     {
    24.         agent.SetDestination(player.position);  // should do the job if your navmesh is setup correctly
    25.     }
    26. }
     
    Last edited: Aug 16, 2020
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Also I'd argue don't use Time.time. It has issues with precision and Unity explicitly says not to use it in per-frame calls (e.g. Update).

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         nextCheck -= Time.deltaTime;
    4.         if (nextCheck < 0)
    5.         {
    6.             nextCheck += checkRate;
    7.             FollowPlayer();
    8.         }
    9.     }
     
    Zer0Cool likes this.
  5. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Oh okay thanks it was in the tutorial I was watching (I'm not a pro at C# yet haha)
     
  6. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I corrected my above code, so no Time.time at all and no update method. The code is working i tested it so go ahead.
     
    PraetorBlue likes this.
  7. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    There's an error now saying
    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
    EnemyAI:FollowPlayer() (at Assets/EnemyAI.cs:34)
    EnemyAI:Update() (at Assets/EnemyAI.cs:27)
     
  8. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    You need a navmesh generated in your scene (with a ground object!)
    https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

    And you need the nav mesh agent is setup up correctly and active in the scene hierachy:


    If you spawn your agent make sure he sits on the ground and so on the navmesh.
     
    Last edited: Aug 16, 2020
  9. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    I followed the building navmesh but it wont bake
     
  10. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    You need at least 1 ground object and set it to static:
    - make a plane (for your level ground)
    - set size of plane 100x100x100
    - set plane to static
    - set other objects to static that should be included for the baking
    - click on Navigation -> Object
    - here you should see at least your plane (= level ground)
    - click on Navigation -> Bake -> click bake
    Now you should see a blueish surface in your scene (this is your nav mesh).
     
    Last edited: Aug 16, 2020
  11. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Thank you soo much!!
     
  12. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Now when the Enemy chases you the map starts to decrease
     
  13. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    What you mean?
     
  14. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
     

    Attached Files:

  15. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I still don't have it, the level has a limit, yes? ;)
     
  16. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Are you asking why the camera moves?
     
  17. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    There's like a green circle there that start to change the scale
     
  18. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Fixed the map scaling thing