Raycast Performance

I am making a heisting game and I’ll will put all guards and civilians into a folder and run a for loop to get all of them or use collection service. I have not tested it yet but how much will it slow down my game if I ray cast about 50 studs in front the guards and civilians to detect the robbers unsing run services render stepped

i can’t exactly tell you how laggy this will be since you’re supposed to measure it yourself to take in mind other processes being done on your game.

however, this can be optimized, using maths!

vectors (positions) have a property called “magnitude”, which measures how long a vector is if you were to put it in a straight line.

in other words, in order to calculate the distance between 2 vectors, you can use the following code:

local Length = (Vector1 - Vector2).Magnitude
-- The order in which you put these vectors doesn't matter, btw ^^

what you can do in order to improve performance by a lot is doing this calculation on every NPC before actually raycasting. if Length is bigger than 50, then don’t raycast and skip to the next one (since the NPC is out of range).

also, instead of raycasting every render step, you should raycast every 0.01s or so. the difference won’t be noticeable for the players but itll be a massive perfomance boost

1 Like

Thank you for the info, I will also take other conditions in to account like if the player doesn’t have their mask on it wont start the ray

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.