Let’s say I have a map and a turret which loops through all the enemy characters and checks if.
They are in range
They are visible (a raycast operation)
Within every frame or in a very short duration
Currently we just do a for loop, check distance check raycast and get the results but would there be any other way to make this Faster? Using any algorithms.
Thanks
Only thing i can think of is saving raycast data in one of 2 ways.
precalculate all points inside the range using raycasts, then save the results and if an enemy is close to those points it probably isnt visible.
Cache raycasts that were just done.
By caching raycasts, you can first look to see if a raycast with the same (or roughly the same) start and end point exists in the cache, and then use that data
But this wont work if you have a dynamic map. What id do then is just increase the intervals between ccasting the raycast, or if the map changes only a few times a minute you can go with the cache approach and clear the cache every time the map changes
Nonetheless, i believe raycast is actually quite fast (and im pretty sure they optimized it recently to make it 50%~ faster?). so as long as you dont spam it every frame you should be good
as an extra, in the case your enemies always follow a strict path (like in a traditional tower defense game), your best bet would be to precompute the raycasts from each turret to the enemy path. A good spacing for points id say be 0.5 studs but that would depend on how accurate you want it to be, as well as the relative scale of your map.