Help!Unity 2D Spider Boss Attack Logic Issues: Spider Stops Attacking and Starts Jumping After Several Attacks

Hello everyone,

I’m developing a Unity 2D game and have encountered an issue with the attack logic of a spider boss. I hope to get some help.

What I’m Trying to Achieve:

  • After the spider boss enters the field, it locates the player’s position using two detection boxes in front and behind.
  • Once the player is detected, the spider rushes towards the player to attack, with an attack pre-cast of about 1 second.
  • After the attack is completed, it enters an attack completion animation.
  • This attack process loops twice; after two cycles, the spider retreats to a safe distance and enters a cooldown state.
  • The spider repeats this operation continuously.

Current Problems:

  1. Inconsistent Attack Cycles:
  • The spider does not perfectly execute the two-attack cooldown method. It often retreats excessively.
  1. Spider Stops Detecting Player:
  • After several attacks, the spider stands still. Even when the player touches the detection area, the spider does not initiate an attack.
  1. Spider Starts Bouncing:
  • After several attacks, the spider starts bouncing up and down with the ground, and I’m not sure why.

Implemented Attack Logic and Transition Conditions:

  • Entrance Phase:
    • The spider plays an entrance animation. After the animation ends, it begins detecting the player.
    • Uses the IsEntranceCompleted() method to check if the entrance animation has finished.
  • Player Detection:
    • Uses two detection boxes (leftCheckBox and rightCheckBox) to detect if the player is within range.
    • If the player is detected, sets readyToAttack = true and triggers the attack pre-cast animation SpiderAttackCast.
  • Attack Phase:
    • During the SpiderAttackCast phase, the spider moves towards the player until it reaches a certain distance (castMoveDistance).
    • The pre-cast lasts about 1 second; after that, the FinishAttack() method is called.
  • Attack Completion:
    • When the attack animation’s last frame triggers, FinishAttack() is called.
    • The attack counter attackCounter increments.
    • If attackCounter >= maxAttacks (set to 2), the spider enters the retreat state and resets attackCounter to 0.
    • Otherwise, it triggers the SpiderBossAttackFinished animation.
  • Retreat and Cooldown:
    • The spider moves away from the player by a certain distance (retreatDistance) and enters a cooldown state.
    • The cooldown lasts for cooldownDuration seconds.
    • After the cooldown, the spider resets relevant states and repeats the attack cycle.
  • Moving to Player’s X-Axis:
    • The spider periodically tries to move to the player’s X-axis position to approach the player.

Transition Conditions and Trigger Logic:

  • readyToAttack:
    • Set to true when the player is detected, initiating the attack pre-cast and movement.
  • isLeaving:
    • Set to true when the attack count reaches the limit (after two attacks), entering the retreat state.
    • Reset to false after the cooldown ends.
  • isCoolingDown:
    • Set to true when entering the cooldown state, disabling attacks and player detection.
    • Reset to false after the cooldown ends.
  • attackCounter:
    • Records the number of attacks.
    • Incremented after each attack is completed.
    • Reset to 0 when reaching the maximum number of attacks.

What I’ve Tried:

  • Ensuring that state variables (like isLeaving, isCoolingDown) are correctly reset after attacks.
  • Changing the movement method to use physics-based movement (Rigidbody2D.MovePosition) instead of directly modifying transform.position.
  • Disabling gravity effect (rb.gravityScale = 0f) to prevent the spider from being affected by gravity.

Persistent Issues:

  • The spider often does not execute the attack cycles as intended and retreats too much.
  • After several attacks, the spider stands still and stops detecting the player.
  • The spider starts bouncing up and down after a few attacks.

Questions:

  • How can I modify the code or animation state machine so that the spider executes the attack cycles as intended?
  • Why does the spider stop detecting the player and not attack even when the player is within the detection area?
  • What could be causing the spider to bounce, and how can I fix this?

Additional Information:

  • Game Type: 2D game.

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.