Week 3 Day 1 - Tracelines in UE4

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=6icACvXUyqQ



Duration: 2:29:18
61 views
0


Today we went through the steps to get a Use key to work in UE4, to wit:


1) First set up a keybind by going to preferences then input and adding a new keybind. I called the action "Use" and assigned it to the key 'e' as well as a gamepad left trigger for whatever reason.


2) Go into the blueprint for the player character and add a node for "InputAction Use". This will get called by the engine any time the player hits 'e' or the left trigger (or something else if the user rebinds it). Have it print "Hello" when they hit 'e' and verify it works.


One caveat here is that you can only have one blueprint consuming user input, so if you do what I did in the video and have multiple blueprints all trying to read the key, only one will work.


3) Make a blueprint for the door to open. Create a timeline. Double click on the timeline node to edit it. Add a float track. Add two points on the track, one with a value of 0 at time zero, one with a value of 90 at time .75. (Feel free to mess with these numbers.) You can do either a linear interpolation or a curve, play with it to see what feels right. Back in the event graph, use the output of the timeline to set the rotation of the door.


Make a new "Custom Event" node and call it DoorUsed or something like that. Hook up the node to the Timeline's play input. Create a new node for when the player ends overlapping the door, and hook it up to the reverse input on the timeline.


4) Go back into the blueprint for the player character and add a line trace by channel node that will fire when the user hits the 'e' key (replacing the earlier print node we had stubbed in there). It should start at the player's camera location (drag in the camera from the top left and choose get world location and attach to the trace start input) and the ending location will be our starting location + the direction we're looking (use Get Forward Location node) * some distance. Make the distance a variable and tweak it until it feels right.


Pull out from the line trace the hit result, and choose "Break hit result". Drag out from the actor on it to see what we hit, and try casting it to our door blueprint, with a "Cast to Door" node (or whatever you called your door blueprint). If it succeeded, then drag out from the actor and choose the name of the Custom Event you made in step three, and that will play the timeline. It the cast failed, then that means you hit something other than a door. In the video I show how you can have a variety of objects you can use in the world.


This approach works reasonably well for a small number of usable nodes, there's better ways for large numbers of usable things in the world.







Tags:
is50a
traceline
line trace by channel
custom events
ue4
blueprints
key binding