Download

Light switch activated by trace+input?

Hey guys,

I’m not a coder. I have experience in modeling and animation, but no “under the hood stuff”. In other words: speak to me slowly.

Okay, my end goal is to look at a light switch and have it turn on with key input. I know I need to use a LineTrace (forObjects?), but haven’t even been able to get that to work. I followed the UE4 documentation to set this up:

I’ve created my own object type - because that seemed to make sense to me, but maybe it isn’t necessary? Anyway, I have this blueprint in my firstpersoncharacter bp exactly like the UE4 tutorial, but I’m not getting any indication that it is working when I test it.

I have placed a single box over my switch (that would be set to invisible render) and set its collision type to ‘LightSwitch’, my custom object.

Nothing is happening. What have I done wrong? Also, is there a better approach? And why does the space between my eyes hurt so much?

Alright, here we go. Pardon me if you need more explanation, on mobile. Would love to help more if you need.

I’ve made something like this before, so I’ll share how I did it.

1.) I wanted to not only to be able to turn on lights with an input, but also open doors, push buttons, etc. The best way to do this is to create a “base” blueprint class from which all of your intractable classes will derive. We’ll call this one, “BaseInteractableActor”

2.) In the BaseInteractableActor class, create a function called OnInteract. this function will be called anytime player presses the interact key when looking at an intractable object, such as a door or a light.

3.) Create a class that derives from this Base Class. We can call this one InteractableLight.

4.) Your InteractableLight class should have its own on interact function. Go ahead and edit it for your light and set it to where when the function is activated the light will turn on or off.

5.) Your idea with the cube to detect if the players looking was a good idea, but Ive found one that actually works better. Again I’m on mobile so I can’t really look up dysfunction, but look for something called a capsule trace. It’s like a line trace but doesn’t require you to look right at something.

6.) When the player presses your key for interacting, do the capsule trace. We like to keep things off the Tick function as much as possible to reduce load. Get the hit return and see if it is an InteractableActor. If so, call its OnInteract.

Ta-da!! Not only do you have a flickablr light switch, but you can also make interactable anything’s now!
Again, if you have any questions, I can hook you up in a bit and even send some pics of a Blueprint graph if necessary.

Thanks for the reply! I’m excited to have a solution outlines. I still need some help, though.

I have everything working up to step 5. Where do I place the capsule trace? In the BaseInteractableActor? Or in the PlayerCharacter blueprint? Or in the level Blueprint?

EDIT: Also, I want to have my light come on via a timeline that quickly increases/decreases the intensity. But it doesn’t appear I can place a timeline within a function. How exactly should I set up my ‘InteractableLight’ BP in this case? Does it need to have a function for toggling the light?

Trace the capsule in your player, and have it be activated whenever you press the interact key.

EDIT: No, wait! We want Sphere tracing! Check up SphereTraceByChannel.

Alright, so I am unfortunately lost. :confused: (Doesn’t help that the AnswerHub seems to be down.)

Here’s what I have so far for the SphereTrace:

I don’t have anything attached to Out Hits because I can’t seem to attach a Hit Break to a sphere trace (“Only exactly matching structures are considered compatible”). So, not sure how to recognize InteractableActor.

Also, once I have that working, how to I execute my IntractableLight BP? I have my point light in the view port and I have the ‘LightToggle’ function toggling the visibility, but not sure what to put in the event graph.

Thanks again!

Check this out. Just threw it together in the FirstPerson template, and it works:

e44acc2b9a78cac932e7e43f6ae0b1f85786cc8a.png

1.) Don’t do MultiSphereTraceByChannel, just do a regular SphereTraceByChannel

2.) I forgot to do it on my own blueprint, but I’d recommend bumping up radius a bit. Just a minor thing, but it will mean you don’t have to look directly at the light, but rather within so many pixels of it…

3.) In the event graph, once you’ve casted your object, just drag out the “As ______” circle output thing to empty space and search for the name of your function. Should show up as an event. Just gotta execute it.

The Result:

Also, my answerhub is acting up, too; so is my Launcher. Are you able to sign in to yours or am I just unlucky?

My Epic Launcher is logging in fine, it is just AnswerHub that is (still) down for me.

Alright, I’m a little closer. I have my sphere trace working; I can get the game to spit out a ‘non-interactable’ string. Still don’t have the second half though.

That has me confused. “Once you’ve casted your object” - I don’t know what that means or how to do it. Is the ‘object’ my interactablelight BP? That gave me this result:

That’s in the Base BP. It isn’t doing anything.

Also, I assume I need to ‘look’ at the blueprint position in the level? (Which is located at the position of the light). How would I look at a switch and have the light turn on?

You want to cast from within your player blueprint.

&stc=1

Drag out the blue “Out Hit” node from your SphereTraceByChannel. You should have the option to “Break Hit Result” if you drag out that blue circle to empty space.

You want to get the actor that you just hit so you can see if it is interactable; to do this, drag out the “Hit Actor” node. You’d plug that into the “Cast To InteractableLight” node.

Doing it by switch requires a bit more, and I’ll do that in later post.

Okay, so doing it by switch.

Remember how I said make a “base” interactable class and to make your light a child of that class?

Here’s why: the light switch can also derive from this class.

1.) Make another child class of your original Interactable class. I called mine InteractableLightSwitch!

2.) Inside this switch class, add a variable. The variable type should be “BaseInteractable” reference, or you could just make it a “InteractableLight” reference. Either works. Click the grid icon next to it to make it an array/list.

&stc=1

&stc=1

Now, having done this in your switch class, add an override function for your OnInteract.

Interactable_Switch02.png&stc=1

You should now have an “Event On Interact.” This will be called when our player looks at your switch and presses the interact button.

Drag this out into the graph, as well as your variable we created earlier to store all your lights. Do a “ForEachLoop” for your Lights array. Drag out your “element” circle and call the OnInteract for it.

&stc=1

If you haven’t already, go into your InteractableLight blueprint and have it toggle your light visibility when it’s interacted with:

Interactable_Switch05.png&stc=1

Finally, in your level editor, you can add whatever lights you want to turn on to your switch.

&stc=1

First, thank you for taking the time to provide this for me. It is really appreciated.

I’ve managed to miss some steps, as it still isn’t working. I’m unclear on a few concepts - and I’m not especially understanding the communication elements between BPs - and I think that is where I am making my errors.

Can you check my work?

First, my Character BP. Pretty sure this is setup correctly, but unsure on where I am casting to first. You mention both BaseInt as well as LightInt - which do I send it to?

Now, the BaseInteractableActor. I’m confused about its purpose. I know it is holding the OnInteract, but does it need to actually do anything? I have nothing in the Event Graph and on the following in the OnInteract function:

On to InteractableLight. I have a function within it toggling the light:

But not really sure how to trigger that funtion…I did this in the InteractableLight Event Graph:

d447d182d62a79a312353529ef4a381c585e9d24.jpeg

Finally, my SwitchBP. I should have that setup correctly.

I definitely think one of my problem lies in not understanding how exactly what the Base BP is doing.

Hate to monopolize more of your time - but I think I’m getting close!

**You’re so close its outrageous!!!

**Everything is right! You got all the right nodes and everything is connected…

…*except one final piece.
*

Your last graph you showed me, the InteractableSwitch Event Graph: you’re doing the foreachloop, and on interact is connected, and it looks beautiful.

All that remains is that your node for OnInteract isn’t actually called. You forgot to drag out the white execution arrow bit. If you don’t know the purpose of that, I can explain, but that’s all you need!

&stc=1

And no cigar! :confused:

Still getting a “No Interactable!” string (hey, at least I know that’s working!)

Okay, so just to be clear. How does the game know what an interactable actor is? How does it recognize the lightswitch as interactable?

Alright, so here’s the basis of how the game should be knowing what the interactable actor is:

When we do our sphere trace, you know we are sending out a sort of “laser beam” from one point to another - in this case, from our eyes to 1,500 units in front of our eyes.

When this trace hits an object, we get a reference to it called the “Hit Actor.”

When we try to cast our hit actor, we tell the engine, “Okay, try to see if this hit actor is a BaseInteractable. If it is, then great! Tell it we want to interact. Otherwise, let us know that we cast ‘failed’ and that this isn’t a BaseInteractable.” The casting is how the game should know if what we’re looking at is interactable.

The only two issues I can think of right now are:

1.) Your light switch actor is not a “BaseInteractable”

2.) We aren’t looking at the light switch.

Can you confirm for me both of those are false? If so, you’ve got me stumped.

Okay, perfect. That’s how I understood it, so at least I am on the right page.

How would I know this? Is it the fact that it is a child of the BaseInteractable? If so, it is. It’s parent class is BaseInteractableActor_C.

I should be? What exactly does the trace bounce off of? The mesh within the blueprint? Here’s my trace hitting the switch:

It’s a little hard to see the switch; it’s the greyish switch plate - the trace is definitely hitting it.

Well, 1.) checks out. As for 2.), it is a little hard to see, but I’ll have to take your word for it.

Man, this is odd. Add this to your character’s blueprint. Drag off your Cast and stuff to the side, and plug this in instead:

Interactable_Switch09.png&stc=1

Holy ****, it’s working!

It was just returning a staticmesh no matter how accurately I pointed, so I threw a big ol’ cube in the BP and that fixed it. I guess with the trace sphere being so large, it was hitting the wall first.

Thanks so much! Not only has it given me a great interaction system, I’ve also gained some blueprint knowledge.

Quick question: Is there a way to have the trace ignore static meshes? As I play around, I find that the trace is sometimes hitting the corner of a wall or something, even though the target blueprint is centered on screen. It be nice if the trace only bounced off of Base Interactables. I see the ‘actors to ignore’ on my trace, but don’t know how to attach all static meshes to that.

Huzzah! I’m glad you found success!

As for ignoring the mesh: Since you are doing the box collision method, (which was probably what I should have suggested from the start, sorry!) I’d recommend switching to a line trace or a 0-radius sphere trace: Since the box for your switch sticks out farther than any static mesh should, it will be the only thing your camera sees.

Sorry, I’m still experimenting with the best way to do this :stuck_out_tongue:

Sounds like a plan!

No worries. I’m glad to understand the process better.

Do a multi object trace instead of a trace by channel? Then just run the outputted array through a forEach loop that casts to interactableObject.