So I have a first person game. What I’m trying to do is make it so that when the player aims the crosshair (which is locked to the center of the screen) at a certain button and presses the e button then it runs the blueprint for my button. I made a line tracing blueprint which is supposed to check if it’s hitting the object and if it is then it runs. Here’s what I have:
What’s happening is that when I press e, it runs regardless of whether the crosshair is pointing at the button or not. I can be anywhere in the level and press e and the blueprint always runs. So how can I change it so that the blueprint only runs if the crosshair is pointing at the button and the player presses E.
UPDATE: So I have yet another attempt at this line trace which still doesn’t work correctly. Here it is:
This one doesn’t work at all unless you change auto receive input to player 0 but then the blueprint runs everytime I click the mouse and for some reason I keep getting errors that say “Accessed None ‘First Person Camera’ from node LineTraceByChannel in graph ‘EventGraph’ in blueprint button2”. What does that mean. That line trace is in the blueprint for my trigger so it should work but it doesn’t.
In your first image the branch is checking if the trace has been done and your forward vector is 5000 so it can probably hit a good amount of things in the scene. So it has been activated regardless of what it’s touching. I’ve been messing around with this today, i’ll post some screens.
This may not be specific to what you need but it will point you in the right direction.
So when my line trace hits an object all I’m doing right now is destroying it (if the right object has been selected).
I have created 2 Functions in my Character blueprint to keep everything a bit cleaner, one called RayTraceCentre and the other PickUpObject.
RAY TRACE CENTRE
Most of this is the same as yours, my forward vector is lower than yours as i want the character to be right up close to the object. In the Line Trace node I have changed the Draw Debug Type to Persistent, this allows you to see the line trace in the scene. At the end if the line trace has hit something I have recorded what it was, if not it keeps it at nothing. Don’t worry about the Print String nodes if you don’t want, all they’re doing is telling me what was hit on the screen for debugging.
P.S. I am using the camera from the character as the reference of where the line trace should begin.
PICK UP OBJECT
So here I am checking if the hit object was the same as the object I want to activate. Referencing the object that was just hit by the line trace and specifying what blueprint I want affected. If the object hit is different to that blueprint it won’t do anything. Then I destroy it. (not specific to yours)
EVENT GRAPH
To get this all working I created those Functions first and then after compiling them you can search for their nodes in the Event Graph. I have it set up with a Xbox controller but you set it up with whatever button you want.
COLLISION
In the first image the Line Trace node uses a Trace Channel, Visibility or Camera, not sure if there is much difference but both worked for me. Make sure your button has a collision component in the blueprint AND that its Trace Responces in the Collision settings are set to Block.
Hopefully this gets you sorted!
Try setting this up the way I have it first as a test thing just so you can get a better understanding of how it works.
Do I even need to use SET for my blueprint because my blueprint goes into a flipflop node which runs the rest of the blueprint so what does that SET even do?
I’m using a variable (Hit Actor) to record the actor that was hit by the trace. The Set node is being used to do that. So I am Setting the Hit Actor to be an object.
You don’t need to worry about it. It just comes in useful sometimes if you want to do anything more with the name of the object. You can do this instead. Don’t worry about the PickUpObject function, just create the cast node and then whatever you want it to do after that.
If the blueprint looks good then is there something else that could be causing a problem? In my event graph I have “left mouse button pressed” and then it goes to the Ray Trace function. If I set auto receive input to player 0 then it still doesn’t work but this time it causes that error “Accessed None ‘First Person Camera’ from node LineTraceByChannel in graph ‘Ray Trace Center’ in blueprint button2”. I made that custom collision with the object where both camera and visibility were set to block all so I’m not sure why it’s still not working.
Cheers, ok so it seems you have put everything in the button blueprint instead of the character blueprint. Copy over all the nodes to the character blueprint.
Also - the Left Mouse Button event doesn’t seem to be working for me, I tried the E Keyboard Event and that works.
I can’t see any collision component on your button blueprint either.
I’ll clean it up and zip the files up back for you.
Finally it works and it also works with left mouse button so that’s all good. The last thing I’m wondering is that I have multiple buttons in my game so I was wondering how do I make it detect which button the line trace is hitting?