• News

• Industries

• Learning & Support

• Community

• Marketplace

Only-when-moving logic: Velocity check vs old/new location check

I have some complicated logic that needs to be fired every ~0.05s, so I try to optimize it. The logic is needed only when actor changes location - what’s the better way to check if actor is moving?

1. Get Velocity -> Get Vector Length -> check if it’s greater than 0
2. Check if GetActorLocation == previousLocation. If not, it means that actor has moved, then set previousLocation to GetActorLocation

Which one will be faster? I know that the first solution can miss some location changes (rarely, if actor would move and stop within this ~0.05s. period - and it’s not a big deal in my case), but mostly I’m curious about speed.

why do you need the vector length in version one. check whether the vector is equal to 0:0:0 with a failure quote. I think that’s the easiest way and it will run the fastest.
but nonetheless I think if you don’t have thousands of actors ticking non of that options should really have a visible impact on your performance if your programming a desktop game. except for youre trying to replicate that changes

You’re right, I’ve previously used the vector length to compare overall velocity values, but in this case comparing Velocity vector to (0,0,0) will be enough

Thanks!

Update: The velocity method works only when using MovementComponent or physics.
… But when we have e.g. a simple cube that moves along spline (via SetActorLocation), it doesn’t work, because GetVelocity always shows 0.
What do you think, is the second option (Check if GetActorLocation == previousLocation. If not, it means that actor has moved, then set previousLocation to GetActorLocation) the best way to check if the cube is moving?

It can work, but it depends on your actor type.
Physics could change only the slightest bit, depending on it’s state.
Imagine the object is moved only 1 unit (for whatever reason, let it be physic). It would still be called.
So i would get the difference between current location and last location, if it’s out of a specific distance, set the last location to the new position and call your logic.

Thanks, indeed it’ll be better to add a distance comparison on top of it