10.05.2015, 16:25
How can I make an actor face the player's direction?
stock Float:SetPlayerFacePlayer(playerid, faceplayerid)
{
new Float:Px, Float:Py, Float: Pa;
GetPlayerPos(playerid, Px, Py, Pa);
new Float:fpX, Float:fpY, Float: fpZ;
GetPlayerPos(faceplayerid, fpX, fpY, fpZ);
Pa = floatabs(atan((fpY-Py)/(fpX-Px)));
if(fpX <= Px && fpY >= Py) Pa = floatsub(180, Pa);
else if(fpX < Px && fpY < Py) Pa = floatadd(Pa, 180);
else if(fpX >= Px && fpY <= Py) Pa = floatsub(360.0, Pa);
Pa = floatsub(Pa, 90.0);
if(Pa >= 360.0) Pa = floatsub(Pa, 360.0);
if(!IsPlayerInAnyVehicle(playerid)) SetPlayerFacingAngle(playerid, Pa);
else SetVehicleZAngle(GetPlayerVehicleID(playerid), Pa);
return Pa;
}
pawn Код:
Alter it to work with actors. |
stock SetActorFacePlayer(actorid, faceplayerid) { new Float:Px, Float:Py, Float: Pa; GetActorPos(actorid, Px, Py, Pa); new Float:fpX, Float:fpY, Float: fpZ; GetPlayerPos(faceplayerid, fpX, fpY, fpZ); Pa = floatabs(atan((fpY-Py)/(fpX-Px))); if(fpX <= Px && fpY >= Py) Pa = floatsub(180, Pa); else if(fpX < Px && fpY < Py) Pa = floatadd(Pa, 180); else if(fpX >= Px && fpY <= Py) Pa = floatsub(360.0, Pa); Pa = floatsub(Pa, 90.0); if(Pa >= 360.0) Pa = floatsub(Pa, 360.0); SetActorFacingAngle(actorid, Pa); }
SetActorToFacePlayer(actorid, targetid) { new Float: pX, Float: pY, Float: tX, Float: tY ; return GetPlayerPos(targetid, tX, tY, Float: targetid) && GetActorPos(actorid, pX, pY, Float: targetid) && SetActorFacingAngle(actorid, -atan2(tX - pX, tY - pY)) ; }