Check whether a vehicle enters a certain radius

Hello all! Ok, so I have a potentially big ask now…

What I want to have happen at the end of this, is when a vehicle that is stored in a table enters a certain radius within the world, it sends a chat message to the entire server.

I’m happy with any response, but the best thing really is a working example that I can learn from, that’s how I learn really. Thank you for reading this :slight_smile:

Been thinking, and could this possibly be better done by using Raycast? If so, could someone assist me with this please

The best way to do this is take two vectors (1. world coordinate 2. vehicle position coordinate) and find the Euclidean distance.

In C# this would look like:

Vector3 worldCoordinate = new Vector3(0,0,0);
Vector3 vehiclePosition = Game.PlayerPed.CurrentVehicle.Position;

float distance = worldCoordinate.DistanceSquared(vehiclePosition);

then you can use this distance as the radius.

if(distance < 3f)
{
    // do something
}

In Lua, I’m not sure. I know that there are natives like Vdist2() and GetDistanceBetweenCoords() but doing it without a native is faster. Something like:

if (Vdist2(posX, posY, posZ, carX, carY, carZ) < 3) then
    -- do something
end

but faster (from the natives documentation https://runtime.fivem.net/doc/natives/#_0xF1B760881820C952)

dist = #(vector3(0.0, 0.0, 0.0) - vector3(5.0, 5.0, 5.0))
2 Likes

Thank you so much for your reply!

I’d considered doing this, but came across two problems…

I want it to constantly be checking whether any vehicle within a table is in the specified radius of the coordinates, which are for an ANPR camera. But, my main concern is performance and if i’m getting the location of a vehicle in the world and attempting to determine whether it’s in the vicinity of these other coordinates, requiring it to be stored in a variable that needs to be updated every now and then would surely cause performance issues? Especially considering there’s going to be multiple zones and multiple vehicles to check.

And my second issue is I have no idea how exactly i’d get the location of the vehicles in the table. I check the natives and couldn’t find anything in regards to position of a vehicle by it’s hash or anything, only positioning of a player.

1 Like

in LUA i use the id returned from creating the vehicle to locate and delete when needed. I am assuming this is its netID but i could be wrong. Either way, it works to return location, accepts entity functions from native.

spawned_carA = CreateVehicle(vehicleA, coordsA, GetEntityHeading(myPedA), true, false)

As per performance i am runnning a huge radial scan on objects and peds, with tables holding upto 1000+ entries at times. “in seperate threads of course”. I have not seen an impact in performance as of yet.

If you are tracking 32 cars in a table, just loop about every quarter of a second, total round would be around 8 seconds give or take a few server ticks.

as per coords you can call the player, if in vehicle and return vehicle id, or use the created return id.
I use. https://runtime.fivem.net/doc/natives/#_0x1647F1CB

GetEntityCoords(vehicleID) -- from created id.