Distance and Length

Hi folks.

I have a couple of AI that have to do a bit of thinking, not every frame, but every few random seconds. Wheh they’re thinking I get a frame rate loss which is very noticeable.

These AI aren’t doing a great deal of thinking, most of it is very simple vector math, but I am relying on Vector2.Distance and Vector2.Length quite a bit. Are these two functions particularly overhead heavy?

How do you guys work with distances, are there any tricks for keeping the processing low?


Do you use the “ref” overrides ?
How many calculs do you do in the update ?
Length and distance can be costly if many items need to be calculated: these use divides and squareroots.
Maybe use DistanceSquare which is faster (Because of no sq call)
I personnally use “fast functions” for squareroots (mainly ported from C++ versions)