Look at door to open

I’m trying to make it so when i am close enough to a door, look at it, and press E it will open

can someone let me know how to create an if statement that is based on looking at an object?

Hello, you would have to do this in the player’s Tick() function as the name suggests, this function will run every tick. In there, you would trace a line and see if it hits the door (A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums). If it is indeed a door that was hit, you should cast the traced actor to a door and then call the “open” method on the door or whatever you are using to open the door.

Here is a code example of how you would check if it’s a door and then opening it (this isn’t valid code, merely an example):

// Assume DoorClass is the UClass that you want the door to be

if (HitData.GetActor()->GetClass() == DoorClass::StaticClass())
{
    DoorClass* door = (DoorClass*) HitData.GetActor();

    if (door && door->IsValidLowLevel())
    {
        door->Open();
    }
}

When you press E, use Line Trace by Channel to check if you’re looking at the door (tutorial: WTF Is? Line Trace By Channel Node in Unreal Engine 4 ( UE4 ) - YouTube).

As for the distance, you can either send the trace with a set length, like 100 or 200 units, or check if the door is close enough to the character after the trace line hits it.

Then execute the door open function. I prefer using interfaces for that, thus you won’t have to Cast to the Door BP Class, just send a message that says “Open Door” and the Door BP performs the function (tutorial: WTF Is? Blueprint Interface - YouTube).

The advantages of this method are a) you don’t use ticks; b) you don’t use casting. Both of them should increase your performance.

You’re right on the not using ticks part, that would save a lot of performance. I was thinking about that he may have wanted a popup if he can use a door (but if he isn’t, your method is better). However for the no casting thing, you’d still have to check if the actor implements the interface, then cast the actor to that interface, so I would say the overhead is about the same in that aspect.

No, just send the unterface message. If an actor does not implement it, nothing happens. That’s the beauty of interfaces,

Ah, are you referring to the static function of the interface prepended with “Execute_” to which you would pass the door as argument?

I believe you’re considering that in the terms of C++ coding, but I haven’t got there yet. In the Blueprint it’s just an Interface Message node that you can send to any actor you like, regardless anything. E.g. I have an interface function named Turn On/Open that is implemented by various different actors, like doors, lights, etc. In the Actor Blueprint there’s a Turn On/Open event node that executes whatever you want, and any other actor can send a Turn On/Open message to that actor. So, for example, when a button is pressed, it sends the message to all the selected actors, regardless their class, and they do what they are supposed to do. Is that what you meant?

Oh ok, thank you for sharing this, useful to know. I was indeed talking about the C++ interfaces, which appear to be a bit different from their Blueprint counterpart.

i ment blueprints