Make a player face another player - 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)
+--- Thread: Make a player face another player (
/showthread.php?tid=350064)
Make a player face another player -
Extremo - 11.06.2012
Hey,
I couldn't find anything on the topic, been searching for a while now and it's probably all over the forums. At least I am sure people have made it before but could someone show me a snippet which calculates the facing angle for Player 2 to face Player 1? That'd be awesome.
If you're at it already I wouldn't mind you explaining the math behind it in a understandable fashion but its not mandatory. Though if I grasped my head around it next time I wouldn't have to ask again haha.
Regards.
Re: Make a player face another player -
MP2 - 11.06.2012
pawn Код:
stock 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);
}
Not made by me. No idea what any of it means.
AW: Make a player face another player -
Extremo - 11.06.2012
Thank you, really appreciated =)
Wouldn't mind someone explaining the math behind it though! :P Oh well, solved unless someone wants to bother.
Thanks again!
Regards.
Re: Make a player face another player -
waxhunter - 11.06.2012
He's using player1 pos and player2 pos to calculate arc tangent (atan), which gives the angle of the tangent (player2posy-player1posy)/(player2posx-player1posx), exactly what you asked for. Z coordinate isn't actually needed, but still you have to use it as GetPlayerPos requires 3 arguments. I wouldn't use floatabs to calculate the angle, though.
Also, this function sets angle for vehicles.