Rotating my object after it reaches it's destination is causing it to move

Hello,

I have an object that is set to move to a point using AIDestinationSetter.target. I understand that pathfinding still occurs even after we reach the destination, and I think this is what my problem is.

Once I have detected that the object is at the destination, I rotate the object to look at the target (for example, an employee walks to a shelf and then turns to face the shelf so they can “stock it”).

When this happens, the object seems to move again and spin around. My theory is that the rotating is nudging the object off of the node, and then the pathfinding kicks in and tries to get back to the node, which causes the object to spin around as well.

If I set “canMove” to false manually in the inspector, they do not do this. This doesn’t really work in script and I rather not toggle canMove on and off to get around this since that could make a mess elsewhere in the code.

I also tried setting AIDestinationSetter.target to null once it’s reached it’s path but the object still seems to want to pathfind to the node it stopped at.

I believe this is a similar issue to this one way back in 2013. I’m not really sure what his solution was though, I think the code might have changed since then.

https://forum.arongranberg.com/t/look-at-on-path-completion-not-working/1106

It may also be similar to this unresolved post as well:

https://forum.arongranberg.com/t/rotate-to-face-object-at-end-of-path/554

Here is my rotation code. I tried a bunch of different rotation methods to try and rule this out. The all result in the same thing. This is placed in a coroutine that stops when the angle between the target and the object gets low enough, but here’s the meat of it, nothing fancy:

        dir = (target.position - transform.position).normalized;
        dir.y = 0; //so we don't tilt up or down
        rotTo = Quaternion.LookRotation(dir);

        transform.rotation = Quaternion.RotateTowards(
            transform.rotation, rotTo,
            Time.deltaTime * turnSpeed);

Thank you in advance.

Figured out how to get around this. Basically in my movement script I was checking to see if the unit was close enough to it’s destination. Once it was I would carry on with it’s other actions, which includes rotating to face it’s target. The key words here are “close enough”. So the unit would start rotating as it would walk it’s last few steps towards the target, which caused all sort of wonky behavior as the rotation code fought against the pathfinding code.

To fix this I added a check for AIPath.velocity.magnititude to be <= 0.1, or essentially not moving. This way they would be pretty much stopped before I started rotating. Works fine now!

Thanks

2 Likes