01.07.2011, 03:28
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.
stock IsPlayerCloseToObject(playerid, objectid, Float:radius)
{
new Float:x, Float:y, Float:z;
GetObjectPos(objectid, x, y, z);
if(IsPlayerInRangeOfPoint(playerid, radius, x, y, z)) { return 1; }
return 0;
}
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));
}
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.")
}
}
}
}
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 Код:
pawn Код:
|