Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Getting the closest set location? [SOLVED]

Featured Replies

Hi, i only ask this after thoroughly racking my brains :tongue:. I want to get the closest of 3 set Vector3 locations the Player is closest to. The way i have been doing it is checking if the player is closest to 1 or 2, and then i check that result to 3, however it always returns that i'm closest to 1, no matter what.

Has anyone done anything similiar and have an easy method? Ask me if i'm not being clear enough!

Thanks.

Edited by AnIdioticMonkey
solved

  • Author
20 minutes ago, ainesophaur said:

How are you checking the distance? There's a method which takes a vector3 and gives you the travel distance

I'm using a DistanceTo() extension for vector3 (got it off of LCPDFR - the stickied thread by LMS)

That's one of the common ways to do it. I would test what results you get with Vector3.TravelDistance()

http://docs.ragepluginhook.net/html/C1AEA24D.htm

http://docs.ragepluginhook.net/html/FF036733.htm

I've used it plenty of times and it's always been accurate for me. I had a list of spawn points that I would loop through and check their distance to the LocalPlayer  

  • Author
18 minutes ago, ainesophaur said:

That's one of the common ways to do it. I would test what results you get with Vector3.TravelDistance()

http://docs.ragepluginhook.net/html/C1AEA24D.htm

http://docs.ragepluginhook.net/html/FF036733.htm

I've used it plenty of times and it's always been accurate for me. I had a list of spawn points that I would loop through and check their distance to the LocalPlayer  

 

18 minutes ago, khorio said:

Vector3.traveldistanceto()

Still no go, it still is setting me closest to location 1. Here is my code - (sorted into 2 voids)

 public void firstloc()
        {
            if(Game.LocalPlayer.Character.Position.TravelDistanceTo(loc1) < Game.LocalPlayer.Character.Position.TravelDistanceTo(loc2))
            {
                Game.LogTrivial("Closer to 1");
                close21 = true;
            }

            if (Game.LocalPlayer.Character.Position.TravelDistanceTo(loc1) > Game.LocalPlayer.Character.Position.TravelDistanceTo(loc2))
            {
                Game.LogTrivial("Closer to 2");
                close22 = true;
            }
        }

        public void secondloc()
        {
            if (close21 == true)
            {
                if(Game.LocalPlayer.Character.Position.TravelDistanceTo(loc1) < Game.LocalPlayer.Character.TravelDistanceTo(loc3))
                {
                    Game.LogTrivial("Closest is 1 - setting");
                }
            }

            if (Game.LocalPlayer.Character.Position.TravelDistanceTo(loc1) > Game.LocalPlayer.Character.Position.TravelDistanceTo(loc3))
            {
                Game.LogTrivial("Closest is 3 - setting");
               
            }
        }

 

Vector3 pos1 = Game.LocalPlayer.Character.Position.GetOffsetPositionFront(50f);
Vector3 pos2 = Game.LocalPlayer.Character.Position.GetOffsetPositionFront(65f);
Vector3 pos3 = Game.LocalPlayer.Character.Position.GetOffsetPositionFront(80f);

float pos1travel = Game.LocalPlayer.Character.Position.TravelDistanceTo(pos1);
float pos2travel = Game.LocalPlayer.Character.Position.TravelDistanceTo(pos2);
float pos3travel = Game.LocalPlayer.Character.Position.TravelDistanceTo(pos3);

if(pos1travel <= pos2travel && pos1travel <= pos3travel)
{
//pos1
}
else if(pos2travel <= pos1travel && pos2travel <= pos3travel)
{
 //pos2
}
else
{
  //pos3
}

 

When you're checking the first distance, you should check it against all available distances (eg, pos2 and pos3). I'd check less than or equal to because if they're equal. Obviously if you have more than a few values to check, this solution wouldn't be the best as it would become very messy to maintain as you add values.

I highly recommend not using TravelDistance in these kind of checks as it takes into account various path nodes.

DistanceTo is highly preferred here and is standard.

My YouTube: Click here. 

My Discord Server - https://discord.gg/0taiZvBSiw5qGAXU

Useful post? Let me and others know by clicking the Like button.
Check out my many script modifications! 
Having issues? LSPDFR Troubleshooter by Albo1125.

  • Author

Thanks @ainesophaur, that works! And @Albo1125 - i wasn't using DistanceTo as that didn't work (spawning this thread). Anyway, thanks for all your help guys!

Edited by AnIdioticMonkey

@AnIdioticMonkey

Add your three locations to a List. Then...

Vector3 closestPoint = myLocationList.OrderBy(x => x.DistanceTo(Game.LocalPlayer.Character.Position)).FirstOrDefault();

It'll return the closest Vector3, or null if nothing was found. 

Edit: I think that's the syntax. I normally code in VB.NET, so my C# is a bit rusty if I don't have Visual Studio in front of me. 

Edited by Stealth22

Stealth22
LSPDFR Tester | Plugin Developer
My Plugins: Code 3 Callouts | Traffic Control | Keep Calm | ALPR+

Please do not PM me for any kind of technical support.
I unfortunately do not have enough free time to answer every PM that I get. For issues with my plugins, please post in the comments section of the file, or it's forum thread. You'll get a much quicker response from me there than if you send me a PM; I do my best to respond to every question in the comments sections. For API/programming questions, please post them in the API Development forum, so all developers can benefit from the answer as well. Thanks!

  • Author
19 hours ago, Stealth22 said:

@AnIdioticMonkey

Add your three locations to a List. Then...

Vector3 closestPoint = myLocationList.OrderBy(x => x.DistanceTo(Game.LocalPlayer.Character.Position)).FirstOrDefault();

It'll return the closest Vector3, or null if nothing was found. 

Edit: I think that's the syntax. I normally code in VB.NET, so my C# is a bit rusty if I don't have Visual Studio in front of me. 

Great, thanks! That's much easier!

20 hours ago, Stealth22 said:

<snip>
It'll return the closest Vector3, or null if nothing was found.
<snip>

Vector3 is a struct, so it will return a Vector3 with X,Y and Z set to 0.0f instead of null since structs can't be null.
But for normal classes it will return null, as you said.

Just now, alexguirre said:

Vector3 is a struct, so it will return a Vector3 with X,Y and Z set to 0.0f instead of null since structs can't be null.
But for normal classes it will return null, as you said.

Right, my bad. I forgot Vector3 is a struct, so it would just return Vector3.Zero.

Stealth22
LSPDFR Tester | Plugin Developer
My Plugins: Code 3 Callouts | Traffic Control | Keep Calm | ALPR+

Please do not PM me for any kind of technical support.
I unfortunately do not have enough free time to answer every PM that I get. For issues with my plugins, please post in the comments section of the file, or it's forum thread. You'll get a much quicker response from me there than if you send me a PM; I do my best to respond to every question in the comments sections. For API/programming questions, please post them in the API Development forum, so all developers can benefit from the answer as well. Thanks!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.