a function that find the closest object to me
#1

There's a function that find the closest object to me ?
Reply
#2

If you use GetPlayerDistanceFromPoint and GetObjectPos, this should be very doable.

pawn Код:
GetClosestObjectToPlayer(playerid)
{
    new
        Float:objPos[ 3 ],
        Float:prevDistance = 999999.9,
        closestObject = -1;
   
    for( new i = 0; i < MAX_OBJECTS; i ++ )
    {
        GetObjectPos( i, objPos[ 0 ], objPos[ 1 ], objPos[ 2 ] );
        new
             Float:distance = GetPlayerDistanceFromPoint( playerid, objPos[ 0 ], objPos[ 1 ], objPos[ 2 ] );
        if( distance < prevDistance )
        {
            prevDistance = distance
            closestObject = i;
        }
    }
    return closestObject;
}
This might be a very unefficient code. It may not even be working. I just got something together that I thought would work.
Reply
#3

How to use it ?
Reply
#4

You could use it as a teleport or a camera command. To view the object or be teleported to it's position, using that function. But I don't see how that function would return any object ID or anything as such to indicate what object was closest.
Reply
#5

That's the problem, I need to get the closest object to me and then to edit it with (EditObject)
Reply
#6

Quote:
Originally Posted by Gennum.
Посмотреть сообщение
How to use it ?
If you look into the function, then it's not too hard to understand.

The only parameter required is 'playerid' - which obviously means that GetClosestObjectToPlayer(0) would return the closest object to playerid 0.

What does it return? The ID of the object.

So let's say you want to move the closest object to a certain position, you'd simply do
pawn Код:
MoveObject( GetClosestObjectToPlayer( playerid ), 0.000, 0.000, 0.000 );
That is, if the function even works as intended.
Reply
#7

Can I use it like that: EditObject(playerid,GetClosestObjectToPlayer(playe rid))
Reply
#8

Yes you can.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)