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.
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?
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.”
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.
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:
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.