When enemy meets dead player it gets stuck on it

Having adjusted the position of the enemy’s two colliders per Rick’s instructions and set them both to triggers I get the player to die whether it walks into the enemy or hits it from above. However now when the enemy re-meets the dead player it gets stuck - it continually flips direction meaning it doesn’t move anywhere.

I’m guessing this is because the enemy’s box collider (the periscope) is now continually exiting another collider (the player’s capsule collider) , and so flipping?

Any thoughts on how I might keep the killing functionality but solve this particular issue?

Thanks,

K

I’m wondering if there is a way for the EnemyMovements.cs script to detect the isAlive variable in PlayerMovements.cs. Then if isAlive = false one could stop detecting the periscope code (put a test !isAlive at the start of the OnTriggerExit2D method).

I’ve tried to pick up the isAlive in the enemy script but keep getting various error messages. Would this work and if so how might I code it?

Thanks,

K

If your main goal is to prevent the odd enemy movement, a simple solution could be to check in the OnTriggerExit2D whether the trigger belongs to the player or not, regardless of whether the player is alive:

    void OnTriggerExit2D(Collider2D other)
    {
        if(other.gameObject.GetComponent<PlayerMovement>())
        {
            return; // Don't do anything further when colliding with the player
        }
        moveSpeed = -moveSpeed;
        FlipEnemyFacing();
    }

Specifically to get access to the isAlive bool, you’ll need to either make it public instead of private or create a method to check the value. You can put this in PlayerMovement:

public bool IsAlive()
{
    return isAlive; 
}

Then a similar check to the one above, but specifically accessing that method:

    void OnTriggerExit2D(Collider2D other)
    {
        if(other.gameObject.GetComponent<PlayerMovement>())
        {
                if(!other.gameObject.GetComponent<PlayerMovement>().IsAlive())
                {
                    return; // Don't do anything further when colliding with the dead player
                }
        }
        moveSpeed = -moveSpeed;
        FlipEnemyFacing();
    }

The first if checks that there is a PlayerMovement component because without one we can’t then try to check for something specific.

There are more complicated ways to do these things as well, but either of these approaches should give you the desired result.

Many thanks for your help. I copy pasted the two sets of code; set up the IsAlive() in PlayerMovement and then the if statements in EnemyMovement. I also set the variable isAlive to public. But whilst the script editor shows no errors I get a compile error when returning to Unity:

So instead I deleted the IsAlive method and then called the variable directly:

if (other.gameObject.GetComponent<PlayerMovement>())
{
    if (!other.gameObject.GetComponent<PlayerMovement>().isAlive)
    {
        return; 
    }
}


And that worked. (The enemy now ignores the dead player)

As a tidy up I then joined the two conditionals:

 private void OnTriggerExit2D(Collider2D other)
  {

      if (other.gameObject.GetComponent<PlayerMovement>() && !other.gameObject.GetComponent<PlayerMovement>().isAlive)
      {
                  return; 
      }
      enemyMoveSpeed = -enemyMoveSpeed;
      FlipEnemyFacing();

   }

Many thanks again for your help - I find the interplay between c# and Unity (getting hold of variables etc) quite confusing.

K

1 Like

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

Privacy & Terms