Get distance between player and specific trigger

Hi There,

How can I get the distance between the player and a specific trigger on key press?

The problem I’ve got is theres multiple triggers under the floor, periodically one pops up into the room, I’d like it so when the player presses “X” it prints the distance between the player and that specific trigger?

Would really appreciate some help, as am pretty lost tbh.

its actually pretty simple to do. all you need is a reference to the trigger and a reference to the player character, then use the “get distance to” node or one of the other similar nodes. if you want the distance to the edge of the trigger volume thats a bit more complex but could be done by adding an offset to the distance result based on the volumes size or you could use a line trace.

250175-capture.png

heres an example of how it could be done in the level bp.

I think the problem is how to get the reference to a specific trigger on the fly, as it wont always be the same trigger that pops up and I need the distance to whichever trigger is up

in that case all you really need to do is modify the script that pops up the triggers so it sets a variable in a location you can access and sets the variable to be a reference. that may sound a bit confusing but its not too bad.

as an example lets say that your script that moves the triggers is in the level bp. in this case you could have a variable in your character that is of the type trigger volume object reference. so when the level bp sets the location of the trigger you can set the variable in the character at the same time. that way you have a reference to the active trigger.

now this is all an example of course and will depend on your personal goals and implementation. if shown the script you have now i could provide more specific details.

Many thanks for that, the script I’m using to raise the triggers is in the level bp and I’m simply using a move component to node to raise the triggers, please bear with me as I’m away from home for a few hours, when I get back I’ll send you a screenshot of the bp itself.

Hi Sorry about the wait, here’s my setup:

This is what I mean by popped up:

your script seems odd, it appears that you are having on overlap with trigger 109, 109 goes down and 110 comes up. so in theory you either only have two triggers or you are having the same script many times, if the latter is the case then there has got to be a better way to go about things.

anyways yea to implement what i showed you would just need to have a variable in the character for the trigger thats up and you would place the script that sets it just after where you have trigger 110 moving. the script you would add is the section shown in my above picture from the get player character to the set node. you would obviously have different names for things in your case though so the cast to thirdperson may be something else like firstperson or whatnot.

You’re right my codes very crude, I have over 130 triggers in my map.

I’ve followed everything you’ve said which I’m massively appreciative of, what do you think of this solution as its what my tiny brain can understand?

Ah ok, many thanks for that, the triggers only switch up when the player hits them, so its only the updating of the value that’s a problem, I hope!

I really appreciate your help, please bear with me for a few days as I’m spending the weekend with my kids away from home, and will get back to you soon after hopefully with some good news!

if you have 130 triggers i would create a more dynamic solution using arrays and such. as for what you posted in the picture it would never work because you are comparing your player location to nothing. you need to have an object to compare to for the get distance to node.

how do you want the system to work? are the triggers going to pop up at random? do they only switch when the player hits them? i would sort out how it all works and get your system better optimized prior to implementing the distance to aspect.

below is an example of how you could implement the whole system in the level bp. the script below handles the raising and lowering for all of the trigger boxes. i set it up so the box selected is random (so its a bit like whack a mole). this way of doing things eliminates the need to write the same script for every trigger in the level. the timelines are just a 0-1 value over 1 second. in the second picture you will notice there is show how you would do the get distance to. anyway the idea is that you have a variable that is the currently raised trigger and use that variable as the thing to compare to in the distance calculation. hopefully this example helps by showing you how it could be done.

the only thing left to script is the calling of the switch trigger event which is based on your gameplay.

250209-capture3.png

k if they switch when the player hits them then thats pretty easy as well but will require a bit of repetitive work (if done in the level bp) as shown with the examples in the below picture. the other option would be to make all the triggers out of a base actor which is a much better way to go. then you could have instances of that one actor bp. this would save you from having to do repetitive work by leveraging the object oriented nature of scripting.

Hi Thompson, I apologise for being a pain, because I’m pretty worried and apprehensive about my coding ability and trying to keep it simple so someone as stupid as me can understand (hopefully without using casting and custom events) what do you think of this, all of which is in the level BP, I’m happy to do the repetitive work?

In theory it should work, but I’m no awesome coder like yourself!

250262-posssol1.jpg

what you have there should work except for one thing, when you subtract the trigger location from the player location it will give you a vector which isnt a good way to tell the player distance. what i would do to fix that is to add a vector length node between the subtract node and the print string. the vector length node will return a float value which is much easier to understand and better for display purposes.

Is this correct?

yea it should work. have you tried testing it? the best way to know if it works would be to test it out and see if it gives you a reasonable number. also note that the number it gives you will be from the origin of the character to the origin of the trigger, so likely the center of each. it also will be calculating a direct line to them not just on the horizontal axis but on the vertical as well.

it shouldnt really add that much to the length but the way to do it would be to remove the z axis. you could do this by multiplying the vectors by another vector of 1-1-0. or instead of using the subtraction and vector length you could just use the get horizontal distance to node as shown in my earlier posts, though this would require you change your script a bit so the variable is of a different type.

Could I please ask what this would look like in my bp, I assume I’d have to do the same to the get player character bit?

It seems to work I’ve tried it about half a dozen times and the values seem correct, but this was before using the vector length node, is there anyway to just get horizontal values without vertical?

Or just make and break the vector and leave the “Z” axis unlinked which will zero it out.

what you show in the picture would in theory be giving you the vector length from the trigger to the world origin, and the player to world origin. thats not what you want. the x input one would be from the player to the trigger. if you want to get the location of something then that will be a vector value, the length is just how long the line is from one point to another. imagine you were plotting a graph in the x and y scale. the x 2 y 5 value would be the location, then if you drew a line from that point to the x0 y0 point, the length of the line would be the vector length.

when you press x your about 300 units from the triggers center right? if so then everything is working as intended.