How bad is raytracing for performance? Any better alternative?

I have a pick up system that checks what objects are in front of the character using a sphere trace by channel that runs at tick. If I have ‘n’ characters this would be very expensive, so I was thinking what other alternatives are there?

An idea came to mind: use a overlap check that the player must trigger pressing a key, such as E, this would trigger the raycasting that would pick up the object that it points at.

The problem with this is that the interactable objects would not be highlighted for picking up until the player would first press E. This has the problem that the same button to detect objects and picking them up would be the same. So the player wouln’t really have a way of selecting which object to pick up if they were close to each other in the world.

Also, since this would be used by NPC’s, pressing E is not an option, hence it would have to be a constant check of overlapping and then fire the sphere trace by channel. Which is kind of worse for performance than just having the sphere trace run at runtime.

Any other idea?

Rather than tracing, you can have a collision shape in front of the player ( box or sphere or capsule tilted by 90 ) that overlaps useable objects.

So the player knows when objects are near, because an overlap occurs. You can highlight on begin overlap, and un-highlight on end overlap.

When the player presses E, then you can line trace to where the cursor is pointing.

This would also work for NPCs, but choosing which object would need to be scripted for them.

OR:

You could use the current system for the player, and some sort of overlap system for the NPCs.

Further still, you can use a custom collision channel for these traces and overlaps, so the system is not constantly tracing on visibility.

1 Like

Why do you think it’s expensive? Also, you can run it 10x per second rather than 60 - it will be unnoticeable. But, as above, having a small collision volume in front of a character that adds / removes (unique) entities to / from an array on overlap is not a bad idea at all.

Sounds somewhat similar to this, it does not even use an array:

The setup is further down in the thread. Would that work for you?

The overlapping would occur on tick right? So the dilema would rather be do I want to be constantly checking for overlap for a sphere or do I want to constantly raycast to check if there is any object in the way?

I have a better idea, let’s see what you think of it with an example:
I have a sword and a bow very close to each other spawned in the world, so if I had an overlapping sphere for both of them maybe I would be in the middle of both and they would get picked up at the same time. Instead, I could set up a ray trace that is acive when pressing E that would help choose which one of the two gets picked up (the one that the raytrace intercepts with at the moment of releasing E). So, while E is being pressed, the raycast would overlap with actors, potentially display their name (or highlight them) and upon releasing E the weapon would get picked up. This could also be generalised to any sort of item that can get picked up. This way I think I would avoid constantly raycasting.

But how to set this up for the NPCs? since they cannot press a button such as E? By the way, my game is singleplayer, so it would only have NPCs. I’m not familiar with AI and NPCs since I still have not reached that point in my game (I’m doing main character), so if you could tell me if my system could be implemented with the NPCs beforehand I could avoid doing mistakes now.

By expensive I mean that it would have a great cost in performance if I had 100 characters all raycasting (1 playable character + 99 NPCs for example). I don’t know for sure if this is true, I’m not an expert, it’s just a guess.

The video you have shown me seems like a good way to do it too. I’ll look into it.

Nevertheless, could you check my comment above and see what would work best? If the video you have shown me or the method I proposed?

This could work. Perhaps you could adapt this pseudo logic:

  • press a key, if there’s an object pick it up
  • if there’s not, highlight stuff nearby
  • you can now mouse ever with more precision and hit E again

It’s not quite what I wanted.
Because if there are 2 objects nearby and I press the key, how do I know which one would get picked? The raycast would have to happen first, not after we check if there are objects or not.

Could you comment on this case I spoke of before?:
“I have a sword and a bow very close to each other spawned in the world, so if I had an overlapping sphere for both of them maybe I would be in the middle of both and they would get picked up at the same time. Instead, I could set up a ray trace that is acive when pressing E that would help choose which one of the two gets picked up (the one that the raytrace intercepts with at the moment of releasing E). So, while E is being pressed, the raycast would overlap with actors, potentially display their name (or highlight them) and upon releasing E the weapon would get picked up. This could also be generalised to any sort of item that can get picked up. This way I think I would avoid constantly raycasting.”

Also, thanks for the answers.

Why both? You get an array of items, it’s up to you to choose which to pick up. The closest to the cursor? The closest to the player? The one that’s a bit higher than others?

  • key goes down, sphere overlap (so you do not really need additional components), get item array
  • key goes up, find the one that you want based on some design criteria mentioned above

This would work pretty well from the UX point of view. They can pick up an item even if they do not click it directly. I’d definitely experiment with this; I like it. The fewer clicks, the better.

Okay, but do you prefer to do this with sphere overlap or with trace by channel?

It would be more or less the same,
press key:

  • overlap: start overlap check or
  • trace: start tracing

lift key:

  • overlap: pick up object based on logic (closest to mouse, etc.)
  • trace: pick up the last object with which the trace collided.

So it is just a matter of what would be more efficient ,overlap or trace?

It’s more about the desired design and functionality, the performance hit of doing a single trace is not measurable in this instance.

  • when you sphere trace you project a sphere from A → B (do you need to check what is between the player and target location, for obstacles mayhap?)
  • when you sphere overlap, it’s just one location, and returns everything found there

Which one of these can I pick up according to the rules of your game providing I’m in range?

What I prefer is to choose the right tool for the job. This is not something that is difficult to re-factor, though. To me it sounds like Trace by Channel is redundant and:

Could work just fine. But again, this would depend on a bunch of factors we do not know about here.


There is also Multi Sphere Trace that projects a sphere and returns multiple overlapping hits.

Okay, thanks for the answer! I’ll try both. My idea is not to pick the closest one, but rather out of a bunch of items that could be at the same relative distance from the character, pick up the one that we are “looking at”. That’s why the raytrace made more sense for me, since you can just point it at whichever actor you want and pick up that one. Also, yes I need to check if there is something in between (such as a wall) to not pick up something on the other side.