Enemy AI Doesn't Work

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;
}

}



You are looking at the player so I guess you should do move forward as well.

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
    Transform player;
    NavMeshAgent agent;
    public float checkRate = 1f;
    float nextCheck;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        agent = gameObject.GetComponent<NavMeshAgent>();
        agent.updateRotation = true; // rotate the agent always to the player
        agent.stoppingDistance = agent.radius * 2f; // we do this so the agent dont move the player away
        InvokeRepeating("FollowPlayer", checkRate, checkRate); // follows the player again every checkRate seconds
    }

    void FollowPlayer()
    {
        agent.SetDestination(player.position);  // should do the job if your navmesh is setup correctly
    }
}

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).

    void Update()
    {
        nextCheck -= Time.deltaTime;
        if (nextCheck < 0)
        {
            nextCheck += checkRate;
            FollowPlayer();
        }
    }
1 Like

Oh okay thanks it was in the tutorial I was watching (I’m not a pro at C# yet haha)

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.

1 Like

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)

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.

I followed the building navmesh but it wont bake

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).

Thank you soo much!!

Now when the Enemy chases you the map starts to decrease

What you mean?

6210623--682442--thanos snapp.PNG
6210623--682445--normall.PNG

I still don’t have it, the level has a limit, yes? :wink:

Are you asking why the camera moves?

There’s like a green circle there that start to change the scale

Fixed the map scaling thing