Hello! So this is the question. I need to pick up items in VR like in the references. Can you give an example or the idea how to do this on blueprints?
Do you mean like a force pull that brings an object into your hand? If so, it’s a little complicated but here are some tips to help you get started:
-
It would be best if all your pickups derive from a common parent blueprint (like an empty BP_Pickup class) so when your hand is searching for a thing you dont have to test individual items one by one. You can reparent objects if you’ve already made some, otherwise make this blueprint first and derive all your object’s children from this.
-
Add two sockets to your hand skeleton and move them out to the palm. One can be used as an attachment point and for orienting the object correctly to the hand. The other can be used to point in the direction you are seeking out objects to pick up and has at least one axis pointing in that direction if this needs to be different than the first socket’s orientation. Let’s say you point with the blue vector so we can trace along that socket’s Up vector later on.
-
Create a input binding for attempting to grab something - like maybe the side grips on your controller for example - to start and stop searching for objects. You’ll probably want to come back here later to add safety checks … such as “dont let me grab other things if something is in my hand already”.
-
When this input is pressed, start a new timer by function name to start shooting out a line or sphere trace. You don’t really want to do this on Tick in a VR game, when a frequency of 0.25 works just as good (4 times a second instead of 60 times a second is better for FPS). When the input is released (or once an object is found) kill this timer.
-
On your timer, create a line trace with a start position of the palm socket used for aiming. Find the socket’s Up Vector (assuming the blue Z axis is pointing in the correct direction) and multiply this by however far away you want to grab things from… then add this result back onto the start location to get the end position for the trace. If the hit result detects an object of your parent pickup class - store that object in a variable, store it’s current position and orientation into a vector and rotator (or transform) and kill the timer.
-
To get that object to fly into your hand, I would suggest using a Timeline next. Set the time length to match how fast you want object to fly (let’s say 2 seconds). [Note: To get more fancy, you can modify the timeline’s playrate based on how far away the thing was originally as homework. The way to access this is to drag the timeline component out to the graph and use the set play rate node off it].
-
Add a point on the track at (0,0) and another at (2,1) to represent going from 0% to 100% towards your hand over two seconds. Bend the curve a bit to scoop upwards if you want it to accelerate.
-
Outside the timeline on its Update execution wire, feed the new track you just made as an alpha pin into a set of Lerp(Vector) and Lerp(Rotator) nodes. In the A sides, use the original position and rotation you stored when you first detected the hit (not its current while its moving). In the B sides, use the current location and rotation of the hand socket you want it to finally attach to. Now use the Set Actor Location and Rotation nodes to move the object towards you. This will not only move the object towards you, but it will also spin the object to the way you want to grab onto it. It might be helpful before running the timeline to turn off the objects physics and collision to guarantee it makes it all the way.
-
On the timeline’s Finished wire, you can then worry about doing any final adjustments on the socket and hard attaching it to your hand or however you want to hold the thing.
-
If anything doesn’t make sense above, you can probably google search for the part you don’t get to find further help. Good luck!
For picking things up something along the lines of this should work:
-Make a socket on the hand that you want to hold the weapon.
-Line trace from the hand constantly to see if there is something near.
-Make two bool values. One should be true if the Line Trace is detecting is an object that you can pickup. The second one should be true if you don’t already have an item in your hand.
-Check for input on the trigger you want to be your pickup button and both bools.
-Connect the line traced object to the socket.
For dropping items:
-When the drop button is pressed detach the object from the socket.
I hope this helped and good luck!