[Discussion] Interacting with environment

Hello. I started working in Unreal and especially game development pretty recently. So interacting with environment is a really important part of games. And mostly all the interactions will be done with one key i.e. “E” key. What kind of system or technique do you guys implement to manage interacting with environment.

This thread is for learning. I will appreciate any kind of knowledge passed on by you guys.

Regards

This may be by no means, the most efficient approach, but it works and there is tutorial support on how to do it. My character class does a line trace directly out from the camera (thus the reticule) for the distance I use as a item/information discovery distance. If there is a hit, I check if the actor that was hit is of a class that wants to be discovered. If it is, the “Press E to” message is presented to the player.

The GetWorld->LineTraceSingleByChannel returns an FHitResult. I interrogate the FHitResult (named “Hit” in this example as follows: if(Hit.GetActor()->IsA(AFlightPawn::StaticClass())) {//this is flying class, ask if player wants to fly it}

After you’ve done the trace, you can interrogate for many types of classes. In my case that would be my wheeled vehicle class, flying vehicle class, weapon class, door class, elevator class, and item class. Then I have one additional class that registers as a valid hit, but does not put a message out. This is for easter egg type of finds, where the hit is noted, but no message is put out unless the player intentionally presses E blindly, allowing there to be many items that the player can check out in world exploration without having messages just pop up continuously (that is to say, a player can look at anything and press E and often will get back information from items that aren’t normal pickups or use items).

The only other significant thing to mention is that the message content comes from the class that was hit. So for example “Press E to fly this craft” would be a public variable embedded in the class that was hit, which can be extracted on hit, like MessageToDisplay=Hit->GetActor()->MessageToDisplay.

I chose to do this in my walking Character class because I didn’t want messages popping up when they were driving or flying. It’s done in the Tick() function of the Character. This might be too expensive, but I will change that if I run into performance issues.

I based this on a tutorial by Tom Looman called UsableItem. Search on those three words with UE4 and you will find it. His method assumes that all usable items are in a single class, but it’s easy to extend so you can discover any type of class.

Hey Suhaib,

for Ego Perspective Games I usually use Line Tracing from the eye position.
for 3rd Person Games you could use a Sphere Collider or a Box Collider which is in front of the Player. (The position and scaling depends on the game)

Greetings