13.07.2013, 23:48
I want that the Npc check if there's a player of X meters and if the player if near of X meters the NPC will follow him
Here's the code
And Here's the GetClosestPlayer and GetDistanceBetweenPoints
Here's the code
Код:
forward FollowPlayer(playerid, npcindex); public FollowPlayer(playerid,npcindex) { for(new i; i < MAX_NPC;i++) { new Float:x, Float:y, Float:z; new Float:nx, Float:ny, Float:nz; GetPlayerPos(playerid, x, y, z); FCNPC_GetPosition(NPCID[i], nx, ny, nz); if(IsPlayerInRangeOfPoint(playerid, 2.0, nx, ny, nz)) { FCNPC_Stop(NPCID[i]); FCNPC_AimAt(NPCID[i], x, y, z, 1); } else FCNPC_GoTo(NPCID[i], x, y, z, MOVE_TYPE_RUN, 0.0, 3); } return 1; }
Код:
function Float:GetDistanceBetweenPoints(Float:rx1,Float:ry1,Float:rz1,Float:rx2,Float:ry2,Float:rz2) { return floatadd(floatadd(floatsqroot(floatpower(floatsub(rx1,rx2),2)),floatsqroot(floatpower(floatsub(ry1,ry2),2))),floatsqroot(floatpower(floatsub(rz1,rz2),2))); }
Код:
stock GetClosestPlayer(playerid,Float:limit) { new Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2; GetPlayerPos(playerid,x1,y1,z1); new Float:Range = 999.9; new id = -1; for(new i; i < MAX_PLAYERS;i++) { if(!IsPlayerConnected(i)) continue; if(playerid != i) { GetPlayerPos(i,x2,y2,z2); new Float:Dist = GetDistanceBetweenPoints(x1,y1,z1,x2,y2,z2); if(floatcmp(Range,Dist) == 1 && floatcmp(limit,Range) == 1) { Range = Dist; id = i; } } } return id; }