Get the camera orientation, then convert that into a look vector.
Place the look vector at the camera eye position
Scale the look vector by the distance you want to search for objects
Apply a “Line Trace by Channel” node, with the eye position and look vector as end points
Grab the “Hit Actor” from the hit result
Cast the hit actor to the light switch blueprint. If the hit actor is a light switch, the cast will succeed. Then, you can activate a bool flag within the player character which enables the “F” input button.
When the player activates their input button, you should send a message to the ‘looked at’ actor and tell it to toggle itself. I recommend blueprint interface messages so that you can have multiple switch types. See the content examples for an example of this.
How can I enable input when a ray hits an object(in my case a lamp) so if I press “F” I can turn the light ON/OFF and disable the input when the ray is not hitting the object so if I’m away and I press “F” I cant turn the lights ON/OFF.
with a helpful message that says “press F” when the ray hits that certain object.
You should create a variable for the currently selected object. If your line trace succeeds and hits a valid object type, then you set the currently selected object to that object. If the line trace hits a non-usable object, then you set your currently selected object to “null”. When you go to interact with the currently selected object, put a “IsValid” macro node before trying to use it.
and heres where I’m having some problem,. so I can enable input but I dont know how to disable it when the ray is not hitting the object. So once the ray hits the object, it stays enable all the time and I can toggle the light even if I’m away.
wow you really are knowledgeable about blueprints and I’m not. I’m really sorry I’m a newbie. you lost me with that, I think I need to learn a lot first before to get that working huh?
Yeah, here is a series of screen shots from my ‘target locking’ system which uses the line traces and target selection, similar to what you’re trying to do. This should help you get on the right track!