Jump to content

Seting a player to face a player


[HLF]Southclaw

Recommended Posts

Hi, i recently started making a server i'm kinda getting to know the code [i'm used to PAWN :D] and I just want to make a server for me and me friends, i want to make a player face another player, i have a formula from a sa-mp function and i want to put it in mta here it is:

stock SetPlayerToFacePlayer(playerid, targetid)
{
 
new
	Float:pX,
	Float:pY,
	Float:pZ,
	Float:X,
	Float:Y,
	Float:Z,
	Float:ang;
 
if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetid)) return 0;
 
GetPlayerPos(targetid, X, Y, Z);
GetPlayerPos(playerid, pX, pY, pZ);
 
if     ( Y > pY ) ang = (-acos((X - pX) / floatsqroot((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0);
else if( Y < pY && X < pX ) ang = (acos((X - pX) / floatsqroot((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 450.0);
else if( Y < pY ) ang = (acos((X - pX) / floatsqroot((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0);
 
if(X > pX) ang = (floatabs(floatabs(ang) + 180.0));
else ang = (floatabs(ang) - 180.0);
 
SetPlayerFacingAngle(playerid, ang);
 
	return 0;
 
}

Link to comment

Just giving some differences with Lua.

Lua doesn't need any variable initializations. Also has dynamic types (no need to use int, float etc).

not is handled by ~= or keyword not instead of !.

Also, nil (in most languages called null) and false are different things.

I'm too lazy to actually help you with this specific problem though.

This is the camera stuff and you can look for other functions (and example usages) in wiki

https://wiki.multitheftauto.com/wiki/SetCameraMatrix

Link to comment

I had a look at those 2 liunks and they are helpful thanks, but none of them are getting me closer to making a player face another, i think the main problem is that i don't understand the formula of the equasion so i can't get it, i will ask on sa-mp forum if someone can explain, people don't usually help though then just say 'search for it' or work it out yourself, i've already done that and i'm not the best at maths lol!

anyway it would be great if someone could help me on this one! :D

EDIT: This is what i made:

function GameLoad ()
ped = createPed(0,0,0,10)
  	local pX
local pY
local pZ
local X
local Y
local Z
local ang
 
if     ( Y > pY ) ang = (-math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0);
else if( Y < pY && X < pX ) ang = ( math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 450.0);
else if( Y < pY ) ang = ( math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0);
 
if(X > pX) ang = (math.abs(math.abs(ang) + 180.0));
else ang = (math.abs(ang) - 180.0);
 
setPedRotation(ped1, ang)
setPedAnimation(ped1, "ped", "WALK_old", 0, 1, 1, 0)
end

does it look like it would work?

Link to comment
function GameLoad ()
ped = createPed(0,0,0,10)
local pX, pY, pZ, X, Y, Z, ang    -- You can define all these vars in 1 line
 
if     ( Y > pY ) then ang = (-math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0) -- Don't forget "then", and Lua doesn't need semicolons
elseif( Y < pY && X < pX ) ang = ( math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 450.0) -- You need to use "elseif" here
elseif( Y < pY ) ang = ( math.acos((X - pX) / math.sqrt((X - pX)*(X - pX) + (Y - pY)*(Y - pY))) - 90.0) end   -- Close the if with an "end"
 
if(X > pX) then ang = (math.abs(math.abs(ang) + 180.0))
else ang = (math.abs(ang) - 180.0) end
 
setPedRotation(ped1, ang)  -- Where's "ped1" defined?
setPedAnimation(ped1, "ped", "WALK_old", 0, 1, 1, 0) -- Where's "ped1" defined?
end

(Text after "--" are comments)

Something like that I guess.

The changes I've made might be a little confusing, but it's how if clauses work in Lua:

if <check> then
<action>
elseif <check> then
<action>
else
<action>
end

I'm also not sure why you did "ped1". I didn't touch it because perhaps you've defined it somewhere else, but in case you don't, change "ped1" to "ped" or vice versa.

Something else quite unrelated, but still... Wouldn't you use math.atan2? Something like here:

findRotation - findRotation useful function on the wiki

You can of course also use that function itself if you want to, it's just that the code in the wiki looks a bit cleaner. :P

(Fun fact: It seems like the example on that page is about the same thing as you wanted. You can change your code according to the Example)

Hope it helps.

Link to comment

wow! thanks for that it's in now and i'm going to test, the code i got now is:

function GameLoad ()
ped = createPed(0,0,0,10)
 
local x,y,z 	= getElementPosition(player);
local tx,ty,tz 	= getElementPosition(ped);
 
setPedRotation(ped, findRotation(x,y,tx,ty) );
 
setPedAnimation(ped, "ped", "WALK_old", 0, 1, 1, 0)
setTimer ( SetPedRot, 1000, 0, ped , player )
end
addEventHandler ( "onResourceStart", getRootElement(), GameLoad )
function SetPedRot( ped , player)
 
local x,y,z 	= getElementPosition(player);
local tx,ty,tz 	= getElementPosition(ped);
 
setPedRotation(ped, findRotation(x,y,tx,ty) );
end

so i got a repeating time that sets the rotation of the ped to the player so the ped is following the player

i'm making a zombie game just for fun as the npcs are a bit messed up on sa-mp

Link to comment

yeah i checked that already I just want a simple one i don't want someone else's script so i just want to make a simple one i made a code now but when i join the game it spawns me at tierer robada not the co-ords i set

here is the code i made

i'm a noob at this!

addEventHandler("onResourceStart", resourceRoot,
function()
for i,player in ipairs(getElementsByType("player")) do
		JoinFunc(player)
end
end
)
 
function JoinFunc(player)
repeat until spawnPlayer ( player, -711+math.random(1,5), 957+math.random(5,9), 12.4, 90, math.random(9,288) )
fadeCamera(player, true)
setCameraTarget(player, player)
 
 
end
function spawn(player)
repeat until spawnPlayer ( player, -711+math.random(1,5), 957+math.random(5,9), 12.4, 90, math.random(9,288) )
fadeCamera(player, true)
setCameraTarget(player, player)
end
 
addEventHandler("onPlayerJoin", root,
function()
	JoinFunc(source)
end
)
 
addEventHandler("onPlayerWasted", root,
function()
setTimer(spawn, 1800, 1, source)
end
)

I removed everything form the resources folder so i just got 'resources/play/play.lua' and i edited that script, i'm missing something! thanks for help! :D

All i want is 5 zombies to spawn when each player joins and for them to chase the player and when they die they just re spawn and start chasing again, is this a bit hard for starting off? i know exactly how to do it in PAWN but the only problem is that npcs are buggy [still in beta >:(]

Link to comment

First you need to learn Lua. Just because you can do it in Pawn doesn't mean you can do it straight away in Lua. All you need to do is learn the basics of Lua and make simple scripts from scratch.

You aim too far for your skills in Lua.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...