Enemy never attacks player

I have reinstated the code as shown in the lecture, but the enemy stops and stares down at the player after chasing it.

I am roughly following Ben’s code, it is here: https://gitlab.com/ShivamMukherjee/tl7_rpg2.git

I tried observing whether any changes to the chase radius or switching weapons (thereby switching weapon attack ranges) affected it, but without avail.

Here’s a video of what’s going on: https://youtu.be/L7tJ6lqqsYs

@ben since there’s very few active members on this course at the moment, I tagged you. Am I missing anything?

is the player assigned a layer that the enemy can / should be attacking?

@Vladimir_Mabhena The enemy finds the player by object reference. Both have a DamageSystem (HealthSystem but I played along with Ben’s oopsie). I’ll test if the enemy ever calls the correct function first.

I found out that I was using a global target variable which had not been assigned a local reference. I think this will solve the issue.

Turns out it was assigned in AttackTarget() (I call it Attack() in my code). I’m not sure what’s causing the issue. The function is never called.

I found out that the weapon’s range was never being assigned. The enemy attack animation is playing and the state is assigned but no damage occurs to the player.

While debugging, only the player’s attacks were triggering breakpoints in the code.

This is the offending code.

private IEnumerator DamageAfterDelay()
{
	yield return new WaitForSecondsRealtime(.5f);
	DealDamage();
  //^^^^^^^^^^^^^ never called after yield directive.
}

I removed this coroutine call and it began working again.

1 Like

Later on, I figured out that StopAllCoroutines is pretty evil, since it causes problems for any coroutine that may ever be played. I went for an approach where I stored the UnityEngine.Coroutine handle of each coroutine to be kept track of as fields in EnemyAI.cs, and stopped these coroutines one at a time using the override for StopCoroutine which uses a Coroutine as an argument.


private Coroutine patrolCoroutineHandle;
private Coroutine chaseCoroutineHandle;
private Coroutine attackCoroutineHandle;

Whenever a state changes, a method which looks like the one below is called.

private void StartPatrolling()
{
	state = State.Patrolling;
	if (chaseCoroutineHandle != null && attackCoroutineHandle != null)
	{
		StopCoroutine(chaseCoroutineHandle);
		weaponSystem.StopCoroutine(attackCoroutineHandle);
	}
	patrolCoroutineHandle = StartCoroutine(Patrol());
}

The drawback to this is that one must remember which MonoBehaviour class called a given coroutine, and stop the called coroutine through this class, otherwise Unity throws up a cryptic coroutine continue failure error, which doesn’t otherwise halt gameplay.

Removing all StopAllCoroutines calls allowed me to use coroutines normally. The idea sprang from Ben’s fix for the scene not reloading when the player was killed.

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

I love the way the forum allows us to format code :slight_smile:

1 Like

Privacy & Terms