Activating an actor with the E key

I’m trying to create an actor that will do something when the E key is pressed, the player is looking directly at the actor and they are within a certain distance.

how do I accomplish this using blueprints?

the perspective of the game is 3rd person (using the example project) blueprint screenshots would be really helpful thank you.

I’d do it in a player controller blueprint. I do not know what do you already have in your project and I do not want to repeat half the documentation, so here are short steps:

  1. Add ‘E’ key input node in the PC blueprint (should be an Action Mapping, but for a quick prototype this will do)
  2. On press, you have to do a trace. Either LineTraceForObjects, or LineTraceByChannel. Which one depends on what is easier for you, I’d make a new channel that only those ‘interactive’ actors respond to. Start should be the position of your camera, End should be a position that you get from ‘Forward vector’ of the camera (feed there it’s rotation), multiplied by the distance you need (eg. 500). Multiply vector * float.
  3. Break hit result, you have everything you need there, including the actor that was found. Obviously check for the ‘Return value’ bool, to know whether anything was found or not.
  4. If found, then cast the object into your custom ‘InteractiveObject’ BP class. All of the interactive actors will have to inherit from this base.
  5. In that base class create a new function that you will call when the ‘E’ is pressed. Call it ‘ActivateObject’ for example. Set it’s collision properties so that it does block the newly created channel. The channel default will have to be ‘Ignore’, so no other actors block the trace. To create the channel go to ‘Project Settings’ → ‘Engine’ → ‘Collision’.
  6. After the cast node from the hit result, simply call that function. Every child BP class will have that function, because they inherit from the base. Each child can override the function in case it needs - ie. play different sound or do something different.
  7. ?
  8. Profit! :wink:

If you’d like me to actually post pics of how it is done it will take some time, but those hints will get you right where you want to go. It takes some setup work at first (base class, channel for trace) but that is one of the ways that things like this are done. You could go with trace for object, but you’d have to create a new object type instead.

If you do not want to do a custom channel, you could simply go with ‘Visibility’ channel and rely on the cast node to filter out the wrong results. Will work, but something may block your vision and you will be unable to activate the item. While in theory this sounds good (you can’t activate what you can’t see), there may be issues with something having visibility channel while it should not (ie. volumes or triggers) thus ‘something’ will block the trace and it will take some time to figure that issue out. If you project is small and simple though, it might work well enough.

Good luck.

Thanks for the answer but I still need some clarification. if you could post pics that would be great. If im reading it right there needs to be two scripts one on the PC that checks that I’m looking at the object and one on the actor itself but I’m not sure how the one on the actor works, could you explain this? thanks again.

I went with the ‘Visibility’ solution. Simpler for you.

Start with the class you need the object to be. Create a new blueprint class, based on actor and do this.

Then you need to setup the Player Controller. Make a new blueprint based on Player Controller.

Then you need to make a new blueprint based on game mode, and choose that Player Controller you have just made as the default player controller. Then on your level, in world settings, choose that game mode. MyPC is the PlayerController I have created. You just set the default after double clicking the GameMode BP.

70007-world+settings.jpg

That’s it. Now you just spawn in the level and move the camera around and press E. You may enable debug trace if you wish (on the Trace node in PC BP). I chose the engine cube mesh as the mesh for the actor.

The next step is to make blueprints based on MyInteractiveBaseClass and change the ‘Activate’ function behavior there. You can lit it up, change mesh, move around, destroy, whatever you feel like.

Awesome! thanks this works really well!