Directly setting rotation of RichAI?

Is there a fool-proof way of setting a RichAI’s rotation manually? I know that I can set enableRotation to false and then just change the transform.rotation. I do it like that to let my character look into a specific direction while it doesn’t walk a path.

But when I then set RichAI’s enableRotation to true again (to let it follow the path correctly), the character seems to try first to snap back to the old rotation from before I did enableRotation = false. It should accept the new transform.rotation from the start, but I can’t set RichAI.rotation because it doesn’t have a setter.

Instead of changing enableRotation and setting the transform.rotation directly I now change canMove and call MovementUpdate() and then change the rotation via FinalizeMovement. I guess that’s the more correct way to do this?

Jup, any custom rotations etc must be done before FinalizeMovement

Hi

Good point, I have now added a setter for the rotation property as well (it will be included in the next beta).
However, note that this will just set transform.rotation directly (unless updateRotation is set to false, but I don’t think that’s the case for you).

1 Like

Hey guys I just found myself in the same position. What’s the status at this moment? (I am using the beta branch). I am making a facing wall check when I stop moving, so I can turn back and look to other places, but when I set the rotation after the AI arrives (I do it on the pathCallback when it returns reachedDestination as true), it does it for a frame and then it goes back to the rotation before that one. What should be the correct way of setting some rotation manually (at least until I get moving again)? I tried the FinalizeMovement but does the same for me.

Thanks!

From what I can find on the forum there is no real answer to this question, seeing as I want to do the same thing my self. You can set the rotation but it will always rotate back to the AI rotation afterwards. Is there no simple way to override the rotation of the ai isntead of just altering the transform?

I faced same issue,kinda bypass it with putting my logic in Late Update but when ai was transversing on NodeLink at the end point it again rotated back to wronng direction. I ended up fixing rotatioon in Update loop when he was transversing to fix it.

Hi

If you want different rotation logic I’d recommend setting ai.updateRotation = false. Then in your update function you can set the Transform’s rotation to whatever you want. You can read from the ai.rotation property to find out what the AI’s rotation would have been if it had been allowed to control its rotation.

Setting the transforms rotation is no problem really. The code I use to do that now is:

public void ForceRotation(Vector3 rotation)
    {
        agent.rotation = Quaternion.Euler(rotation);
        aiPath.enableRotation = false //Without this it simply rotates back to whatever direction it was facing
    }

I think the problem is that there is something I don’t understand about how this works. In my mind setting agent.rotation should set the agents rotation, but it seems to only set the transforms rotation. Meaning it will rotate back in whatever direction it was facing.

If you do as you suggest and set ai.updateRotation = false, or simply disable rotation. That works UNTIL you have to re-enable rotation again, then the actual ai rotation is still facing the last direction it was facing, even though the transform looks correct.

If im being unclear I can post some screenshots.

Hi

The agent always rotates towards the direction it is moving in. That’s the only rule it has.

I have found the solution. Basically, the AI’s rotation is stored on this Quaternion on AIBase called simulatedRotation, and when the AI starts moving again, it starts rotating, but not based on the current transform rotation, but the last value of simulatedRotation. This value is protected, but we can simply create a setter on the AI’s rotation property, as Aaron said he would do on a post in this thread but probably forgot xD

I added this line on AIBase.cs:

After setting your custom rotation code, make sure to do this, so the simulatedRotation starts on your current transform rotation, not on where the AI stopped rotating internally.

Easy fix :slight_smile:

Alas, as of the latest BETA, (which I recommend to all RVO users) this fix above does NOT work because of the way RichAI rotation filtering works in the new version.
To fix this 100% in the BETA 4.3+ (the above fix is on the 4.2 version), follow my steps:

As you will edit some files in your project, I recommend backing up your stuff to void myself of any responsibility.

Part 1: Enabling source code editing
First and most important, as you have to edit some source code from the asset, and the current beta package is being considered a library and not an asset, you are unable to edit the files (unity gives out the error " Immutable asset(s) were unexpectedly altered", and undoes any changes you made).

WARNING: if you do this, you will be unable to update your package! I hope Aron sees this and figures out what to do so we can all enjoy freely rotating RichAI without the agent snapping back to the last path rotation once it starts moving again, while being able to update to the latest beta :slight_smile:

So, after downloading the beta from the package manager, COPY the Astar folder out of your project (it is under “Library\PackageCache” inside your project folder under a weird name NOT on the customary assets folder.), then on the package manager under My Registries, find the Astar beta and uninstall.

After the uninstall, close unity and move the Astar folder you copied from the Library folder into the customary Assets folder, wait for compile and if your visual studio gives any errors just regenerate the project files on Preferences:
image

Once you do that, you will be able to edit the source code without unity undoing any changes.

Part 2: The fix
On RichAI.cs, make this thing public.

on AIBase.cs, change the rotation property setter to this, and you will be able to directly set the simulatedRotation regardless of whether or not you have updateRotation on or off.

finally, while enableRotation is false, you can do this:


I have butchered my code here to make it understandable, basically you have to set the richAI.rotation property to the current rotation after changing it, and clear out the filterstate Vector2.

Voila! your character should keep the same rotation when starting a new path.

2 Likes