Jump to content

How to set a ped facing a player?


bamby12

Recommended Posts

Posted

is it possible to make a ped face a player using setPedRotation?

what im trying to do is make a ped spawn in front of a player then face him.

Posted

This should work, thanks.

EDIT: ok im trying to createped with a timer and make the ped face the player, heres the code i used.

  
function peds() 
local x, y, z = getElementPosition ( source ) 
local target = createPed(math.random(0,150), x+5, y, z) 
  
    if not target then 
        outputChatBox("Invalid player", player) 
    end 
  
    local x,y,z     = getElementPosition(target) 
    local tx,ty,tz  = getElementPosition(player) 
  
    setPedRotation(target, findRotation(x,y,tx,ty) ) 
   end 
  

i get 2 errors, bad argument @ getelementposition

and

attempt to perform arithmetic on local 'x' a nil value

any help appreciated guys.

Posted

both your errors are caused by invalid source variable. (and why are you using source AND player variable?)

where is the code that calls this function?

Posted
  
function enterVehicle ( theVehicle, seat, jacked )  
        outputChatBox ( "You sit in the car and suddenly feel extremely drunk!", source )  
    end 
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), enterVehicle )  
  
function peds() 
local x, y, z = getElementPosition ( source ) 
local target = createPed(math.random(0,150), x+5, y, z) 
  
    if not target then 
        outputChatBox("Invalid player", player) 
    end 
  
    local x,y,z     = getElementPosition(target) 
    local tx,ty,tz  = getElementPosition(player) 
  
    setPedRotation(target, findRotation(x,y,tx,ty) ) 
   end 
  

thats the full code.

Posted

well that part has nothing to do with peds() function.

it cant be the full code, since peds() function is never called in this code, so you cant possibly have those errors shown.

Posted

Ok you got me :P this is the full code:

  
function enterVehicle ( theVehicle, seat, jacked )  
        outputChatBox ( "You sit in the car and suddenly feel extremely drunk!", source ) 
        cheesetime = setTimer(peds, 2000) 
       --add effects after, this is what i tried to delete seeing as you dont need it. 
    end 
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), enterVehicle )  
  
function peds() 
local x, y, z = getElementPosition ( source ) 
local target = createPed(math.random(0,150), x+5, y, z) 
  
    if not target then 
        outputChatBox("Invalid player", player) 
    end 
  
    local x,y,z     = getElementPosition(target) 
    local tx,ty,tz  = getElementPosition(player) 
  
    setPedRotation(target, findRotation(x,y,tx,ty) ) 
   end 
  

Posted
function enterVehicle ( theVehicle, seat, jacked ) 
        outputChatBox ( "You sit in the car and suddenly feel extremely drunk!", source ) 
        cheesetime = setTimer(peds, 2000,0,source) 
       --add effects after, this is what i tried to delete seeing as you dont need it. 
    end 
addEventHandler ( "onPlayerVehicleEnter", getRootElement(), enterVehicle ) 
  
function peds(player) 
local x, y, z = getElementPosition ( player ) 
local target = createPed(math.random(0,150), x+5, y, z) 
  
    if not target then 
        outputChatBox("Invalid player", player) 
    end 
  
    local x,y,z     = getElementPosition(target) 
    local tx,ty,tz  = getElementPosition(player) 
  
    setPedRotation(target, findRotation(x,y,tx,ty) ) 
   end 

Should work.

Posted

add this

  
function findRotation(x1,y1,x2,y2) 
  local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
  if t < 0 then t = t + 360 end 
  return t 
end 
  

to your script

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