Check distance between player and multiple objects in array

I made a door that opens when the player is within a certain distance and presses E. I’m trying to extend this functionality to all interactive doors in the level so I’ve made an array of them, but I’m getting the error that the array object reference is not compatible with the single object reference on the “Get Horizontal Distance To” node. How can I check the distance between all the doors?

Note: The open door function is on the door BP, but everything else is on my first person character BP.

Do not use tick for this. Add a sphere collider component to the door (set its collision profile to “overlap only pawn”) and check for your player overlap.

On overlap begin if the player is the other actor (check by cast, interface or actor tag) set player near = true, on overlap end set it to false.

ps

you can only get horizontal distance to a single actor => you are trying to hook up an array of actors.

This is simply not the way, You want all your code to be infinitely scalable, The way I would do it is a trace line or trace sphere every 0.2 seconds or so, this will be for all interactions of the entire game.

When overlap happens you get object, check name, if door you show interact GUI.

When you press E you play animation on object that the trace line is currently hitting “fire a new line to be sure” once hit happens you can trace every frame “every tick” for accuracy but when nothing of value is detected you need a slower loop otherwise your game will become a lagfest .

As for distance you subtract 2 world locations vectors and then use node “Vector length”, its helpful for other things just not for doors.

I followed these instructions, but I’m still having the same core problem: it only works perfectly for the first door. According to my debug text everything is firing as it should; PlayerNear is being set true/false for both doors and the interaction command only gets confirmed when it’s set to true, but the second door doesn’t open (even though the debug text says the movement is completed). Any ideas?

Why do you want to link the other door from openning when your are at the first door?

Why does the door array & distance check matter as a gameplay mechanic?

I got rid of the array. All I have now is a collider on the door that detects collision with the player and updates the PlayerNear bool variable, as you suggested. The collision detection works on all the doors in the level, but only the first door actually opens, which is the same problem I had when I was using distance detection instead of collision detection. The reason I made the array in the first place was because I thought the ActivatedDoor object reference variable could only hold one door in the level as a single type.


If you have other doors that inherit from the base door then in their activation / open function you can right-click on the node and select Add call to parent function.

It should pop up as a brown node. This will call the function in the parent class if only the parent has the logic to open / close the door.

Ok So I looked back on your BP

On begin play do not gather the doors. You will only always get the first one.

Instead do a line trace in front of the character to get the door and set it to the the activated door variable.

Or better yet just add a sphere on your character and test if they overlap a door. If so then set the active door variable. On end overlap set it to nothing.

Which of these nodes are on the player character BP, and which are on the door?

The issue is that on BeginPlay you’re finding all the doors, and then only saving the first door (Get 0) into the ActivatedDoor variable. Then you’re opening that door, so it’s always opening the first one only.

You should update the ActivatedDoor variable and set it to whichever door the player has just gotten near to. Instead of setting the PlayerNear variable in the overlap detection event, you can set the ActivatedDoor variable and pass a reference to “self” (supposing these nodes live in the door BP, that would be the door itself).
In EndOverlap, you can set the ActivatedDoor variable to empty.
And in the Interact event, you can check if the ActivatedDoor is set or not using the IsValid node before trying to call OpenDoor on it.

Let me know if it’s clear or I can try to replicate the set up on my side to share you screenshots of the node configuration I’m proposing.

The get all actors of class BP_ActivatedDoor and open door when PlayerNear=true functions are on the first person character bp.

The collision detection and open door functionality are on the door BP.

1 Like

Ultimately I wound up scrapping the code and following this tutorial to get my desired functionality. I’ll lead this thread live with a tip for fellow new programmers who may run into the same issue: just because something works with one object does not mean it will scale up. Sometimes you just have to start over.

Thanks to everyone who replied!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.