Multiple Enemies attacking simultaneously?

Hi everyone!

I’ve made a mistake in my code somewhere. As my first enemy is completing their first action (but will not be finished their first or second), my second enemy starts their routine.

Where in the code is the check that the second enemy is doing to ensure the first is done? I had to alter my “Take Damage” code, so I’m sure this is what is causing my problem.

Thanks!

There is no place in the code where the second enemy checks if the first is done. Enemy turns are controlled by a state machine in EnemyAI.cs.
The enemies are in a ‘wait’ state until it becomes their turn, at which point it transitions to a ‘Taking Turn’ state. Here, the first enemy will do 1 action. While it is doing this action, the state transitions to ‘Busy’. When this action is complete, state goes back to ‘Taking Turn’ and, if possible, the first enemy will do a second (and third, etc) action until it has no more action points left. Then the second enemy will start this process. Once all the enemies are done, the state will go back to ‘wait’.
This is where you may want to check your code. The state machine transitions from ‘busy’ to ‘taking turn’ when the action completes and executes the onActionComplete action that was passed to the selected action. If your second enemy starts an action early, it’s probably because the state has transitioned back to ‘taking turn’ before the action was completed, or possibly didn’t transition to ‘busy’ at all

2 Likes

Thanks for the hints - I’ll start with the Debug.Log commands on the state to figure out what’s going on :slight_smile:

My favorite way of debugging. If that doesn’t get you there, paste in your EnemyAI.cs script and we’ll see if we can spot it.

1 Like

The debugging solved it - the issue was the float time between states. When I set it to 5f (which is way too long), it was obvious that the code was perfect.
Thanks for your help!

2 Likes

Excellent!
Often setting test values to extremes will confirm code is working and help you get things sorted.
One trick I learned when dialing in settings is to push the setting until the point that it’s notably “odd”, and then dial it back 20%.

2 Likes

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

Privacy & Terms