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:
- Inconsistent Attack Cycles:
- The spider does not perfectly execute the two-attack cooldown method. It often retreats excessively.
- 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.
- 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 (
leftCheckBoxandrightCheckBox) to detect if the player is within range. - If the player is detected, sets
readyToAttack = trueand triggers the attack pre-cast animationSpiderAttackCast.
- Uses two detection boxes (
- Attack Phase:
- During the
SpiderAttackCastphase, 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.
- During the
- Attack Completion:
- When the attack animation’s last frame triggers,
FinishAttack()is called. - The attack counter
attackCounterincrements. - If
attackCounter >= maxAttacks(set to 2), the spider enters the retreat state and resetsattackCounterto 0. - Otherwise, it triggers the
SpiderBossAttackFinishedanimation.
- When the attack animation’s last frame triggers,
- Retreat and Cooldown:
- The spider moves away from the player by a certain distance (
retreatDistance) and enters a cooldown state. - The cooldown lasts for
cooldownDurationseconds. - After the cooldown, the spider resets relevant states and repeats the attack cycle.
- The spider moves away from the player by a certain distance (
- 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
truewhen the player is detected, initiating the attack pre-cast and movement.
- Set to
isLeaving:- Set to
truewhen the attack count reaches the limit (after two attacks), entering the retreat state. - Reset to
falseafter the cooldown ends.
- Set to
isCoolingDown:- Set to
truewhen entering the cooldown state, disabling attacks and player detection. - Reset to
falseafter the cooldown ends.
- Set to
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 modifyingtransform.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.