AIBase rotates to previous rotation after setting isStopped to false again

Hello :slight_smile:

We’re using AIBase.isStopped for stopping our characters. We now have a special case where the movement is stopped and the characters are rotated to face the opposite way before isStopped is again set to false. The problem is that the agent will rotate back to the direction it was facing before setting isStopped to true.

Manually setting rotationFilterState and rotationFilterState2 to Vector2.zero seems to fix this issue but is there a better/proper way of handling something like this? (those two values are currently only reset in OnDisable())

Hi

Could you elaborate? I don’t quite understand the issue you are describing.

Lets say we have a character and a house that they can enter:

  1. We let the character move to the front of the house and stop them using isStopped = true. The character faces the door at this time.
  2. We then manually change the position in code to make the character move into the house.
  3. The character then leaves the house and for that we turn them 180° so they face away from the door.
  4. We enable movement again using isStopped = false
  5. The character turns on the spot to the last rotation before stopping it, which was facing the door.

Am I missing something that I need to call before enabling the movement again or should I not use isStopped in this scenario?

Hi

I could definitely handle this case better. I’ll make a note of it.
However, in your case, you probably want to disable the whole component, not use isStopped.
When using isStopped, the agent still performs movement calculations (including gravity, rotation, and local avoidance). So it is not guaranteed to stay in the same spot. Disabling the component completely is best if you are playing some kind of animation for entering the house.

Thanks for the quick response!

This indeed fixes the issue with the rotation but it also throws an error after the second round of disabling and enabling:

I am disabling it by calling AIPath.enabled = false. Can this be caused by disabling and enabling them too fast or accessing the component while it is disabled for e.g. setting isStopped ?

(We also have a Seeker and AIDestinationSetter component on the GameObject, which are currently not being disabled)

That’s strange. When exactly are you disabling it? During which method call? Do you get any other exceptions before that?

We disable it using an event e.g. when the character reaches the front of the house and we enable it again after the character has left the house (another event triggered by player interaction).

The disabling of the AIPath component happens directly after the character has reached the house using an event we fire with the overwritten AIPath.OnTargetReached() method. Do we have to wait for something else to happen before we can safely disable it after it reached the target?

There are no exceptions besides that but once it appears for one agent, other agents also throw it on disabling