Check if player is in range of 150 to other object

Any Ideas how can I check if the player is in a range of 150(this statement must return true or false)?

Place a Sphere Trigger volume at radius 150 (or maybe a little more to account for the space the object takes up within it) on the level.

Place it over or attach it to the object.

Select the trigger volume.

Open the level BP.

Right click (and this will only work if your trigger volume is already selected) and type “overlap”. Choose Add On Actor Begin Overlap.

It will now trigger an event in the level BP when your character overlaps with it, at the radius you set.

Also, I want to test it while I am inside the trigger not while I am entring or leaving the trigger

Sphere Trigger Volume is just empty class with Sphere component, you can attach sphere component on any class/blueprint if needed

using a sphere trigger is a way. or maybe you can also just use the tick function to make a gradual check (just with some sort of distance check algorithm like player->GetActorLocation().size() - target->GetActorLocation.Size() < .

with that, if you use that for AI. you can also make a decorator with that kind of mechanism.

Good point, thanks

With your volume selected, in the level BP right click and type “overlap”. This time, choose Get Overlapping Actors. With this you can get anything currently within the volume and work with that however you need to.

And like mentioned, you can create a BP out of the object and add a Sphere component to it. You’ll be able to access all of those overlap events for the object and its sphere within that new BP. So it’s up to you if all this resides in an object BP or the level BP.

You can do this in your character’s tick event OR within the other object’s tick event depending on the use case.

If the check is done within the tick event of other object, do this:

  1. Get a reference to your player (lets call it player)
  2. Find the displacement (self->GetWorldLocation() - player->GetWorldLocation())
  3. Find out the distance (Abs(displacement->length))
  4. Now you can use a Less Than node and plug-in the length we calculated in previous step against 150
1 Like

Actually what I am trying to do, is to search if the player in inside that sphere then it checks if player is pressing a key, if yes, it plays a sound(NPC Interaction)

Ah okay. That information is what I needed.

First, establish a variable in your player BP that is toggled depending on if you are pressing the key in question. Something like key Is Pressed.

In the level BP, or wherever you decided to setup your sphere trigger volume, check that the overlapping actor is in fact the player by checking its class. If this is true, activate a looping Timeline.

Do this by right clicking in the BP and creating a Timeline. Double click its node to go into its editor. Check the Loop checkbox at the top. This is now essentially Tick behavior that you have great control over. Play around with Timelines and you’ll quickly see how useful they can be.

On Update of the Timeline, it will fire every frame until the Timeline is told to stop. So you’ll want to plug in On Actor End Overlap to stop it, so it’ll stop when your player leaves the radius.

Next, you will cast to the player. You’ll use its class name, like from earlier. Use your overlapping actor for the cast object, since you’ve already checked its class. From the cast, check for key Is Pressed. If this returns True, fire into a DoOnce and play your sound. False, reset the DoOnce. This is to keep it from playing the sound over and over every frame while the key is being held. Once the key is let up, it allows the key to be pressed and play the sound once again.

Here are pics to help you. Let me know if you need more info!

34532-screenshot+(55).png

I couldn’t find “OnActorBeginOverlap” and “OnActorEndOverlap” Events

Remember what I said earlier: you have to select the sphere in the level. Then, when you right click in the level BP, you’ll be able to get those events

Thanks it Worked!