Vehicle Range Problems
#1

I'm trying to make a trunk and hood command, that works with the closest vehicle, but if a player is in a certain range of two vehicles then it sends a message saying so, to prevent confusion between close vehicles.

Here's the code:
pawn Код:
if(strcmp(cmd, "/trunk", true) == 0)
    {
        new Float:hX, Float:hY, Float:hZ, vehicleid = INVALID_VEHICLE_ID, carcount = 0;
        for(new i = 0; i < MAX_VEHICLES; i++)
        {
            GetVehiclePos(i, hX, hY, hZ);
            if(IsPlayerInRangeOfPoint(playerid, 4, hX, hY, hZ))
            {
                vehicleid = i;
                carcount++;
            }
        }
        if(IsABike(vehicleid) || IsABoat(vehicleid) || IsAPlane(vehicleid) || IsAHelicopter(vehicleid) || IsATrain(vehicleid)) return 1;
        if(GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && vehicleid != INVALID_VEHICLE_ID && carcount == 1)
        {
            new engine, lights, alarm, doors, bonnet, boot, objective;
            GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet , boot, objective);
            if(doors) return SendClientMessage(playerid, COLOR_GRAD1, " This vehicle is locked.");
            if(boot != 1)
            {
                boot = 1;
                format(string, sizeof(string), "* %s opens the vehicles trunk.", PlayerName(playerid));
                SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, 1, objective);
                ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
                return 1;
            }
            else
            {
                boot = 0;
                format(string, sizeof(string), "* %s closes the vehicles trunk.", PlayerName(playerid));
                SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, 0, objective);
                ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
                return 1;
            }
        }
        else
        {
            SendClientMessage(playerid, COLOR_GRAD2, " You are not near a vehilce with a trunk, or you are to close to two vehicles.");
        }
        return 1;
    }
The problem is, even if I'm only close to one vehicle, it still says I'm to close to two vehicles.
Reply
#2

If it detects the CLOSEST vehicle, then the 2 vehicles would have to be EXACTLY the same distance away(To the point), i find that very unlikely to happen, if it does, the stock is not going to give you 2 vehicle ids.
Reply
#3

Well it doesn't detect the closest vehicle but that's what I want it to do, if you read the code you'll see I don't use a stock, and it checks if a player is in a range of radius 4 of a vehicle.
Reply
#4

Try this:

Код:
for(new i = 0; i < MAX_VEHICLES; i++)
{
    vehicleid = INVALID_VEHICLE_ID;
    GetVehiclePos(i, hX, hY, hZ);
    if(IsPlayerInRangeOfPoint(playerid, 4, hX, hY, hZ))
    {
        vehicleid = i;
        //carcount++; (God knows what this was doing)
        return vehicleid;
    }
    return vehicleid;
}
Reply
#5

Wouldn't be better to use a function that detects the nearest car?
Reply
#6

Quote:
Originally Posted by Viniborn
Посмотреть сообщение
Wouldn't be better to use a function that detects the nearest car?
You need to create one, before you may use it.
Reply
#7

With the Pythagorean Theorem you can solve it easily ..
Reply
#8

Missed school days oops..What was that theorem?
Reply
#9

Quote:
Originally Posted by Viniborn
Посмотреть сообщение
With the Pythagorean Theorem you can solve it easily ..
May you please show us an example?
Reply
#10

He was actualy taking about this..
So the right form of Pythagorean Theorem is x*x + y*y = z*z (math example)
But, I think it would be better to do something like checking the distance from vehicle with form..

Код:
stock CheckPlayerDistanceToVehicle(Float:radi, playerid, vehicleid)
{
	if(IsPlayerConnected(playerid))
	{
	    new Float:PX1,Float:PY1,Float:PZ1,Float:X1,Float:Y1,Float:Z1;
	    GetPlayerPos(playerid,PX1,PY1,PZ1);
	    GetVehiclePos(vehicleid, X1,Y1,Z1);
	    new Float:Distance = (X1-PX1)*(X1-PX1)+(Y1-PY1)*(Y1-PY1)+(Z1-PZ1)*(Z1-PZ1);
	    if(Distance <= radi*radi)
	    {
	        return 1;
	    }
	}
	return 0;
}
And you can check the distance between vehicle and player :3 simply and it's working
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)