how to get player's nearest Object id
#1

for example,i used new xx = creatObject(987,x,y,z); to create the object, when a player get near to the xyz coordinate, and type in a command , the screen should appear a message saids your nearest object id is:blabla and the blabla should be xx
Reply
#2

Easy as 1, 2, 3 to find. Just use search!

pawn Код:
stock GetPlayerClosestObject(playerid, exception = INVALID_OBJECT_ID)
{
    new Float:Distance, target = -1;

    for(new v; v < MAX_OBJECTS; v++) if(IsValidObject(v))
    {
        if(v != exception && (target < 0 || Distance > GetDistancePlayerObject(playerid, v)))
        {
            target = v;
            Distance = GetDistancePlayerObject(playerid, v);
        }
    }
    return target;
}


stock GetDistancePlayerObject(playerid, objectid)
{
    new Float:Floats[7];

    GetPlayerPos(playerid, Floats[0], Floats[1], Floats[2]);
    GetObjectPos(objectid, Floats[3], Floats[4], Floats[5]);
    Floats[6] = floatsqroot((Floats[3]-Floats[0])*(Floats[3]-Floats[0])+(Floats[4]-Floats[1])*(Floats[4]-Floats[1])+(Floats[5]-Floats[2])*(Floats[5]-Floats[2]));

    return floatround(Floats[6]);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)