Enemy Ai

Your code is fine. At least, I cannot spot any problem. From what I understood, your enemy moves with base offset set to 5. If that’s correct, your code obviously works.

In answer 15 in this thread, you shared a screenshot. The enemy looks as if its bottom part is stuck in the navmesh.

image

My Enemy starts to float over the ground and is still static.

What do you mean by static? In Unity, ‘static’ usually refers to the Rigidbody or the ‘Static’ setting in the Inspector. A moving collider must not be set to ‘static’.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? For example, does the Update method get called? If so, does the first if-block get called, and does isDetected get set to true?

The enemy chases or attacks the player only if EngageTarget(); gets executed.

Also try to reassign the Target by dragging and dropping the ‘player’ from your Hierarchy onto the Target field of the EnemyAI component.

Sorry with late reply. Have ongoing exams :grimacing:. I meant static generally like not moving. I realise that it was bad word choice. How will test Debug.Logs. Is there a tutorial or is it very simple? I make sure that I always have player(Transform) as target in the EnemyAi component too. Thank you

No worries. I hope everything is going well for you. :slight_smile:

Debug.Log was introduced in the very first video on how to write a C# class. ‘Analyse the situation with Debug.Logs’ sounds fancier than it is. What I meant is: You know the expected logic flow of your code. However, since a lot is going on at runtime, and since we do not know what Unity does behind the scenes, we cannot tell if the ‘perfect code’ gets executed as we think it gets executed. By adding Debug.Logs, we can check if our code gets executed in the expected order, and if the relevant code blocks get executed.

In your case, the problem is that the enemy apparently does not detect the player. That’s what you perceived while playing your game. However, you cannot see what’s going on in your code. The definition of ‘detecting the player’ in the code is (targetDistance <= chaseRadius). How can you check if this condition gets evaluated to true at runtime? By using Debug.Log:

Debug.Log("targetDistance <= chaseRadius: " + targetDistance <= chaseRadius);

What if it never gets evaluated to true? Well, then you could check the values of targetDistance and chaseRadius. When moving the player around, the value of targetDistance should change. If it does not change, the code might reference the wrong object in target. A common mistake is to add the player prefab to target.

By using Debug.Logs, you’ll get information in the console. You interpret that information to narrow the problem down. At the moment, we still do not know if the problem is in the code, or navmesh, or if it’s something else. Since most parts of Unity are not open-source, the Unity engine is often a black box for us. That’s normal, so don’t worry if collecting information feels a bit underwhelming. :slight_smile:

1 Like

Hi Nina, I’m back!
Yes the condition is always false as ‘’‘targetDistance’’’ is staying at a constant value. Screenshot below. How do I not add player prefab to ‘’‘target’’’ but add the player properly?

Thanks

Welcome back! :slight_smile:

targetDistance remains constant even though you move the player?

If so, the problem might be target. Maybe target references the wrong game object. Log the following into your console in the Start method of the EnemyAI class: Debug.Log("target: " + target.name, target.gameObject);. When the message appears in your console at runtime, click on it once. The game object associated with target.gameObject will get highlighted in your Hierarchy. Then check if that’s really your player and if the position values change while the player is moving.

If the position values don’t change, target does not reference the moving player. This can happen if you, for example, reference the root game object of the player but move a child.

Also log the value of chaseRadius into your console. For testing purposes, you could set it to 1f to see what happens. If the code starts working as expected, you know that the problem is targetDistance, not some unexpected behaviour in the code.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms