How to get the distance between two player characters?

I need a way to get the distance between two player characters, the problem is: They’re not spawned in the level yet so I can’t have a reference to them, do I have to create an array of these characters when they spawn and then calculate the distance between them? How can I achieve this? Thanks in advance.

Hey @Chaos_Bunneh! Welcome to the forums!

So this question needs a little bit more information to fuel a good answer. The thing to ask now is “When do you need this distance” and “Why do you need this distance”.
The reason for the “When” determines what kind of event to use and when to call that event, and the “Why” determines how the code needs to be written as there are SO many ways to go about it.

Let us know and we’ll get back to you soon!

Thanks for the reply, I’ll try to better explain:
Using blueprints, I have a system that spawn two players characters at predefined locations (Player Start Points) in Event BeginPlay coded in Game Mode. I need another system (running like Event Tick) to be calculing the distance between them also in Event BeginPlay before they get possessed. I need this for fixing a bug I’ve found, when the characters are too far way from each other they glitch when colliding with the camera colliders.

Okay! So you can just use a test method that you can delete or unhook afterward.

First, on the game mode, create an event. Call it “Start Player distance check” or something. Off of that, use a “DoOnce” node. Then Get All Actors of class with the input of your player characters. Use that array and get indexes 0 and 1 (there should only be two players for this) and set them to two variables like “Player1” and “Player2”.

Next you’ll use a “Set Timer By Event”. You’ll want to create another custom event for that, and set the time on the timer like .3 or so, looping.
From there use “GetDistanceTo” on your two stored variable actors, and printstring!

To run this, just cast to the gamemode on your player character and have it run your StartPlayerDistanceCheck Event! Since it has a DoOnce node, it won’t force it to restart the loop when the second player loads in a fraction of a second later.
image

Also can you clarify on them colliding with the “Camera Colliders”? Cameras generally don’t have colliders at all, so that has me wondering.

Give it a try and let us know how it goes! :slight_smile:

Your method works like a charm! Thank you very much! And the camera colliders are just invisible walls placed on the camera bounds to prevent the player characters going outside of the bounds. Again, thank you, have a nice week!

1 Like

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