Is this possible?
#1

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.
Reply
#2

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;

Reply
#3

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

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.")
            }
        }
    }
}
Reply
#5

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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)