Enemy Ai

Hi, I don’t know why my Enemy is not detecting my player whatsoever. It can definitely move since I tested it moving forwards and backwards. I think there is something wrong with my code but I’m not sure. I would appreciate some help, Thanks.



What’s the chaseRadius set to in the inspector?

Also, please don’t paste code as screenshots. See here how to format code: How to apply code formatting within your post

Sorry about the screenshots. Thanks for the reply


public class EnemyAI : MonoBehaviour
{

[SerializeField] Transform target;
[SerializeField] float chaseRadius = 5f;


NavMeshAgent navMeshAgent;
float targetDistance = Mathf.Infinity;
bool isDetected;
//Start is called before the first frame update
void Start()
{
    navMeshAgent = GetComponent<NavMeshAgent>();
    
}


// Update is called once per frame
void Update()
{
   targetDistance = Vector3.Distance(target.position, transform.position); //3D vector
   if  (targetDistance <= chaseRadius ) 
   {
       isDetected = true;

   }
   if (isDetected)
   {
       EngageTarget();
   }

}
private void EngageTarget()
{
    if (targetDistance >= navMeshAgent.stoppingDistance)
    {
        ChaseTarget();
    }
    if (targetDistance <= navMeshAgent.stoppingDistance)
    {
        AttackTarget();
    }
}

private void ChaseTarget()
{
    navMeshAgent.SetDestination(target.position);
}

private void AttackTarget()
{
    Debug.Log(name + " has found and is attacking " + target.name);
}

void OnDrawGizmosSelected()
{
    // Display the Chase radius when selected
    Gizmos.color = Color.black;
    Gizmos.DrawWireSphere(transform.position, chaseRadius);
}

}

The chaseRadius on the inspector is set to 5.
Thank you.

Hmmm. I tested the code and it works fine. I didn’t get attacked, but that was because the stoppingDistance on my navmesh agent was set to 0. Setting it to 0.1 solved that problem. It’s simply because the distance will almost never be less-than or equal to 0. I’m not sure what else to tell you. Is the NavMeshAgent component enabled on the enemy? Is the speed more than 0?

Thank you again for replying. It actually might be because the NavMeshAgent is not enabled on the enemy although I’m not sure why not since it is a component on the Enemy. Here is the ss for The inspector on Enemy:


At this point I’m just so confused why.
Thanks

Based on the screen shot, it looks like the enemy is below the floor. It may not be registering as on the Navmesh.

Hi, Thank you for the reply. I changed it so it’s resting on the plane yet it still doesn’t work. I’m so confused as to why my enemy isn’t detected or why it isn’t detecting my player. I might have to restart everything unfortunately.

I took another look at the screen shot and it seems the gizmo for the chase radius is vertical. Could it be that it needs to be horizontal? It has been awhile since I did this part. Note: if you add the tag from the lecture you are on the TA will be notified and can probably help you more than I.

1 Like

Hi Christopher,

I’m sorry for the late reply. @edc237 is right. Since there is no tag, I didn’t notice your thread. I’ve just added tags for you. :slight_smile:

Make sure the enemy’s collider is above the ground. Otherwise, the enemy remains stuck at runtime, Have you already tried to rebake the navmesh?

Hi Nina, Thank you for the reply. How do I make sure the enemy’s collider is above the ground. I’m very new to this! Thank you

Here I have the screenshot of my collider settings:


Thank you

I’m not sure about this one. I thought the gizmos was just a big sphere with a radius which I set- it’s chase radius. Still stuck on this :grimacing:

Your screenshot looks fine so far. Is the enemy above the ground while your game is running?

Did you try to rebake the navmesh?

Where is the enemy while your game is not running? Could you share a screenshot of the enemy in the scene?

I keep rebaking the navmesh. Here is the screenshot of scene when not running. Thank you!

Your screenshot looks good but I cannot see if the enemy is really above the ground. It’s bottom part must not be inside the navmesh while the game is not running. Move it a bit higher.

Try the following: In the NavMeshAgent component, increase the Base Offset. Set it, for example, to 5. Then test your game again to see what happens. If the enemy starts to float over the ground

Does your enemy have a Rigidbody attached? If so, check if ‘Is Kinematic’ is enabled.

Also see this thread (and the comments):

I’ve changed the base offset. My Enemy starts to float over the ground and is still static. Initially I didn’t have Rigidbody attached to Enemy. I have now added it and checked ‘Is Kinematic’. I ran it and still same result. I have read the thread, thank you for sending it over, Kinematic is now selected but sill same result. I’m confused.


Thanks.

Your screenshot looks like a step forward. We can see that the cyliner (green outlines) is standing on the navmesh. That’s the most important part because the mesh (capsule) is just decoration.

Since increasing the base offset made the mesh fly, decrease the value until you are happy with the result. With 1, the enemy was stuck in the ground. With 5, it floats above the ground, so we can assume that the ‘perfect’ value is between 1 and 5.

Will this perfect value allow my enemy to move you reckon?

‘Perfect’, not perfect. :wink:

If you test your game with the floating mesh (base offset: 5), you should see that the cylinder (green outlines) moves along with the floating mesh. If that’s the case, you tweak the base offset value until you are happy with the result. Maybe test 1.5 or 2. The goal is to make the enemy move on the navmesh. Once that works, the rest is fine-tuning until the enemy looks as if it is moving on the ground.

Always bear in mind that the scene view is not the real world. We fake a lot of things to make them look good. Aim for a good looking result.

Hi, my enemy is not moving at any .1 value between 1 and 2. Is my code is correct right? posted a while back. Why is my enemy not moving on the navmesh I wonder? I have not got it to behave like it should at all during this project.
Thanks

Privacy & Terms