SA-MP Forums Archive
Is this possible? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Is this possible? (/showthread.php?tid=265456)



Is this possible? - AgentZero - 01.07.2011

Hi all i'm currently trying to figure something out, is there anyway to detect if a player is close to an object? This object will be moving so i need something else other than IsPlayerInRangeOfPoint.


Re: Is this possible? - Shadoww5 - 01.07.2011

This will check if the player is near to the object (within radius):

PHP код:
stock IsPlayerCloseToObject(playeridobjectidFloat:radius)
{
    new 
Float:xFloat:yFloat:z;
    
GetObjectPos(objectidxyz);
    if(
IsPlayerInRangeOfPoint(playeridradiusxyz)) { return 1; }
    return 
0;




Re: Is this possible? - AgentZero - 01.07.2011

ok can you give me a small example of how to use it please.


Re: Is this possible? - BigETI - 01.07.2011

You could use a timer to update the distance check between the player and the object and second I've just made a stock to get the distance between a player ID and an object ID.

pawn Код:
stock GetPlayerDistanceToObject(playerid, toobjectid, &Float:Distance)
{
    new Float:pX, Float:pY, Float:pZ, Float:oX, Float:oY, Float:oZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    GetObjectPos(toobjectid,oX,oY,oZ);
    Distance = floatsqroot((oX-pX)*(oX-pX)+(oY-pY)*(oY-pY)+(oZ-pZ)*(oZ-pZ));
}
Usage:

pawn Код:
new object;
//...
stock GetPlayerDistanceToObject(playerid, toobjectid, &Float:Distance)
{
    new Float:pX, Float:pY, Float:pZ, Float:oX, Float:oY, Float:oZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    GetObjectPos(toobjectid,oX,oY,oZ);
    Distance = floatsqroot((oX-pX)*(oX-pX)+(oY-pY)*(oY-pY)+(oZ-pZ)*(oZ-pZ));
}
//...
public OnGameModeInit()
{
    //Your stuff...
    object = CreateObject(3851, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 300.0);

    //Timer stuff
    SetTimer("CheckPos", 40, true);
    //...
}

//Somewhere in your script
MoveObject(object, 400.0, 100.0, 10.0);

//Then..
forward CheckPos()
public CheckPos()
{
    for(new playerid = 0; playerd < MAX_PLAYERS; playerid++)
    {
        if(IsPlayerConnected(playerid))
        {
            new Float:distance;
            GetPlayerDistanceToObject(playerid, object, distance);
            if(distance < 20.0)
            {
                if(GetPVarInt(playerid, "IsNearObject") == 0)
                {
                    SetPVarInt(playerid, "IsNearObject", 1);
                    SendClientMessage(playerid, 0xFF0000FF, "The Object is in your near.");
                }
            }
            else if(GetPVarInt(playerid, "IsNearObject") == 1)
            {
                SetPVarInt(playerid, "IsNearObject", 0);
                SendClientMessage(playerid, 0xFF0000FF, "The object is now far away.")
            }
        }
    }
}



Re: Is this possible? - AgentZero - 01.07.2011

Quote:
Originally Posted by BigETI
Посмотреть сообщение
You could use a timer to update the distance check between the player and the object and second I've just made a stock to get the distance between a player ID and an object ID.

pawn Код:
stock GetPlayerDistanceToObject(playerid, toobjectid, &Float:Distance)
{
    new Float:pX, Float:pY, Float:pZ, Float:oX, Float:oY, Float:oZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    GetObjectPos(toobjectid,oX,oY,oZ);
    Distance = floatsqroot((oX-pX)*(oX-pX)+(oY-pY)*(oY-pY)+(oZ-pZ)*(oZ-pZ));
}
Usage:

pawn Код:
new object;
//...
stock GetPlayerDistanceToObject(playerid, toobjectid, &Float:Distance)
{
    new Float:pX, Float:pY, Float:pZ, Float:oX, Float:oY, Float:oZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    GetObjectPos(toobjectid,oX,oY,oZ);
    Distance = floatsqroot((oX-pX)*(oX-pX)+(oY-pY)*(oY-pY)+(oZ-pZ)*(oZ-pZ));
}
//...
public OnGameModeInit()
{
    //Your stuff...
    object = CreateObject(3851, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 300.0);

    //Timer stuff
    SetTimer("CheckPos", 40, true);
    //...
}

//Somewhere in your script
MoveObject(object, 400.0, 100.0, 10.0);

//Then..
forward CheckPos()
public CheckPos()
{
    for(new playerid = 0; playerd < MAX_PLAYERS; playerid++)
    {
        if(IsPlayerConnected(playerid))
        {
            new Float:distance;
            GetPlayerDistanceToObject(playerid, object, distance);
            if(distance < 20.0)
            {
                if(GetPVarInt(playerid, "IsNearObject") == 0)
                {
                    SetPVarInt(playerid, "IsNearObject", 1);
                    SendClientMessage(playerid, 0xFF0000FF, "The Object is in your near.");
                }
            }
            else if(GetPVarInt(playerid, "IsNearObject") == 1)
            {
                SetPVarInt(playerid, "IsNearObject", 0);
                SendClientMessage(playerid, 0xFF0000FF, "The object is now far away.")
            }
        }
    }
}
very very nice thank you