Support Forum

Velocity.magnitude not working

I’ve seen this asked a few times, but there hasn’t been an answer. I’m referencing velocity.magnitude for my animator.

When I do the following code, the magnitude still reads as the value before it stopped, so not 0.

_aiPath.isStopped = true;
_aiPath.canMove = false;

I’m not sure why this is. Seems like a straightforward thing so when I stop the agent, the velocity should be 0. The character isn’t moving at all in-game, but the magnitude is stuck at whatever speed they were going before it stopped. Meaning the animation of walking still plays even though the agent should have frozen in place.

Hi

CanMove is perhaps not the best name of that field. What it does is disable all movement calculations. However, it is often used when people override movement calculations (see AIBase - A* Pathfinding Project) and in that case the agent will actually move and update its velocity even though canMove is false.

If you want to stop the agent I would recommend using either isStopped if you want it to stop gracefully and also react to local avoidance, or you can set ai.maxSpeed = 0 if you want it to stop moving immediatelly.

ok cool. wasn’t sure if adjusting maxspeed like that was smart. Ill try that.