Make a player face another player
#1

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

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)