Raycasting: Where to draw the line?

Since I’ve always heard (and sometimes experienced) that Raycasting can be very taxing on a machine, I’m curious at how much Raycast is too much per frame. I’m really only concerned with PC games.

I looked around and couldn’t find any specs on where to “draw the line” in terms of going overboard with casting, so I was hoping someone here can point me in the right direction or at least share their personal experiences with it.

All puns intended. Thanks for the help!

Good question… I say, don’t draw the line at all! Use it at will, don’t think it’ll give you any problem. Unless you want to do something really really REALLLY crazy, otherwise… no fear!

Or… you could test the limit yourself… put a raycast in a for loop, and see how it affects performance on diferent raycast per frame… and if you do please put the results!

Mr Profiler tells you where the line is. For some ballpark numbers, on a simple scene, Unity can process about 100,000 rays per second. (Edit: numbers are on a four year old desktop).

raycasting is pretty fast tbh… Ive never encountered a slowdown from ray casting and I’ve gone pretty nuts with it before.

I think one time I had like 200 ray casts going on an iPad 2 and it was dropping to like 40-50 fps

You draw the line from your origin to the direction vector.:roll_eyes:

Wow, and I’ve always worried about running 10-20 raycasts at once. I think I over-estimated the “expensiveness” of raycasting.

Damn, you beat me to it!

I’m guilty of this as well. I’ve been working on coding some AI though and I realized that to get a really good cover system, I’d need a lot more than 10-20 raycasts per frame, at least working within my knowledge. I’m glad I made this thread though, I’ll be trying some heavier raycasting soon I think.

You can have particles collide with raycasts. They raycast a lot, also I can have at least 500k particles with collision and i get about 40 fps. That means 500,000 raycasts per second on my rig. Dont worry. Very efficient.

Pre-mark cover locations. Only do LOS checks every few seconds. Stagger the initial timers with Random.value, so they aren’t all happening at once. Works fine for me. :slight_smile:

Just use the mask field, so it doesn’t attempt to raycast against every collider in the universe, only colliders of a layer you actually need to. Staggering is an option if you’re doing hundreds on mobile.

I definitely am pre-marking locations. The bigger thing that worried me was detecting which way to lean/pop out of cover. I also wanted to do multiple raycasts (about 4 or 5) to check the cover in order to make sure most of the “body” of the AI is covered. Either way, it sounds like I have plenty of room to play with Raycasting.

Just randomly pick one. Firing from 3 inches to the right isn’t going to change much. And, just make sure the cover spots are placed properly, and you should be fine with the whole body being placed. Plus it’s super satisfying to nail an enemy over cover.

I’m not sure what you meant “randomly pick one” for. If you mean to pick a cover location, I’ve already got that algorithm working. I’m more trying to refine on the staying in cover algorithm. I need him to detect if he’s being flanked, which basically means more of his body is becoming exposed. I’m thinking if more than 2 of those 4-5 casts comes out with the player being able to shoot him, it might be time to try finding a better piece of cover.

I mean randomly choosing a location to fire from. As for detecting if it’s flanked, just doing a cast from the character’s center of mass should do the trick.

Well, I’d like the AI to lean/uncrouch out of cover only if it will give him a shot on where he last saw the player. That’s why I was going to raycast first to decide. I don’t want him just randomly leaning left when the cover is a right corner. Is there a better way to detect this?

Add a couple booleans for each cover object. CanLeanLeft, CanLeanRight, CanUncrouch, and so on. A person isn’t going to be able to tell whether or not he can see the player without sticking his head out to check anyways.

Thanks for the suggestion. And yeah, I am okay with him leaning out and not getting a shot at the player, I’m just not wanting him to do a left lean when it’s obviously not going to give him any sort of view. Your suggestion fixes that though.

Thanks!

(Also, Paragon Evolved looks awesome so far!)