I previously messed around a bit with trace functions inside UE4's blueprint without achieving anything really useful but today was different. What I had in mind for today was to implement the "item pick up" mechanic. An item is highlighted when it enters an area near the middle of the screen. From the get go I knew I wouldn't be using a simple line trace since I didn't want the player messing around too much to get a single little piece of coin perfectly in focus. I used a sphere cast with a radius of 7, not too big so it doesn't overlap several items at once but enough to make it so it's not a chore to pick up a very small item. Here's a list of what I was able to achieve today.
- Highlight an item when sphere cast touches an item.
- Item un-highlights when view is no longer focuses on item.
- Created an object channel called "Item" and set the trace function to respond only to "object type: Item" (for now).
- Saved only once the name of the item in focus (for optimization purposes... you don't want to be casting to the item in focus each frame).
- Set the material only once on the item newly in focus (for optimization purposes... you don't want to set the dynamic material's scalar parameter value each frame. Only once suffise).
- Made a bounding box around the items that enables the player to trace. When the player isn't near an object he isn't tracing and checking for potential collisions. This is for optimization reasons (I still need to understand if a trace is really that expensive on it own... I'm guessing it becomes very costly when it collides with a bunch of different things... but anyway I need to read on that).
- Resolved an issue: two very close items, one highlights but the other one isn't when hovering over and the first doesn't un-highlight. This is due to the fact that the trace function returns "true you are hitting an object" and when I tilt my view a little on the other very close object it is still telling me "true you are still hitting an object". So the first item doesn't un-highlight and the other one doesn't highlight. The thing here is that you get each frame the name of the item it is hitting (you're not casting or anything it just reads the value) and compare it with the item name that was previously in focus. If the two objects are not the same purge the item name you saved and replace it with the current one.
- Modified the player controller to "destroy" the object that is in focus when E is pressed. It basically calls a function that is "runned" onto the server saying "hey I'm a Client... Server could you please tell this item to the be destroyed when I press E".
The awesome in all of that is that it works almost flawlessly and I did it in less than a day (with little prior knowledge... I do learn very quickly). Here's a video example. It looks dumb... but there's so much stuff going on in that little action, especially when it's in a networking situation.