Jump to content

Head Rotation


Sasu

Recommended Posts

I have this script:

function onClientLookAtRender() 
    for i,v in ipairs(getElementsByType("player")) do 
        local rotcam = math.rad (360 - getPedCameraRotation (v)) 
        local xpos,ypos,zpos = getPedBonePosition (v, 8 ) 
        local xlook,ylook,zlook = xpos - 300*math.sin(rotcam), ypos + 300*math.cos(rotcam), zpos 
        setPedLookAt (v, xlook, ylook, zlook, -1) 
    end 
end 
addEventHandler ("onClientRender", root, onClientLookAtRender) 

But I saw that it does not work. The localPlayer works correctly but the other players only see to north, I think. Can you help me?

Link to comment

Because client-side functions are only triggered to the remote client. You need to make it synchronize it with all players by hooking client- and server-side with a timer. Or you could also set element data on each player with the head rotation and in the render function you fetch all streamed in and on screen players and their head rotation and then set it.

Link to comment
  • Moderators

Well triggerClientEvent or elementdata.

- TriggerClientEvent uses 3+ times more data but you can choose what player receives the data. (the closes players)

- ElementData uses less data but it will send to all players directly.

It depends on this:

- 30+ players and for big maps (or full gta san) use triggerClientEvent.

- Less players and players are all in the same part of the world, use elementdata.

Use the command: /shownetstat to choose between those ways of data transfer.

Link to comment

I tried with this but it doesnt work:

function onClientLookAtRender() 
    setElementData(localPlayer, "CRotation", getPedCameraRotation(localPlayer)) 
    local cx,cy,cz = getPedBonePosition (localPlayer, -- s8) -->
    setElementData(localPlayer, "CRotationX", cx) 
    setElementData(localPlayer, "CRotationY", cy) 
    setElementData(localPlayer, "CRotationZ", cz) 
    for i,v in ipairs(getElementsByType("player")) do 
        local CCam = tonumber(getElementData(v, "CRotation")) or 0 
        local rotcam = math.rad (360 - CCam) 
        local xpos,ypos,zpos = tonumber(getElementData(v, "CRotationX")), tonumber(getElementData(v, "CRotationY")), tonumber(getElementData(v, "CRotationZ")) 
        local xlook,ylook,zlook = xpos - 300*math.sin(rotcam), ypos + 300*math.cos(rotcam), zpos 
        setPedLookAt (v, xlook, ylook, zlook, -1) 
    end 
end 
addEventHandler ("onClientRender", root, onClientLookAtRender) 

Then I tried with this other one but it doesnt work too:

Server:

function rotateHead() 
    for i,v in ipairs(getElementsByType("player")) do 
        triggerClientEvent("rotateHead", v) 
    end 
end 
setTimer(rotateHead, 3000, 0) 

Client:

addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function() 
    local rotcam = math.rad (360 - getPedCameraRotation (source)) 
    local xpos,ypos,zpos = getPedBonePosition (source, 8 ) 
    local xlook,ylook,zlook = xpos - 300*math.sin(rotcam), ypos + 300*math.cos(rotcam), zpos 
    setPedLookAt (source, xlook, ylook, zlook, -1) 
end) 

Link to comment

Something like this?:

Server:

function rotateHead() 
    for i,v in ipairs(getElementsByType("player")) do 
        local _,_,_, x,y,z = getCameraMatrix(v) 
        triggerClientEvent("rotateHead", v, x, y, z) 
    end 
end 
setTimer(rotateHead, 3000, 0) 

Client:

addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(rx, ry, rz) 
    setPedLookAt (source, rx, ry, rz, -1) 
end) 

It says in debugscript 3: Bad Argument @ 'setPedLookAt'

Link to comment
addEventHandler("onClientRender", getRootElement(), function() 
  
for k,player in ipairs(getElementsByType("player")) do 
if getElementHealth(player) >= 1 then 
local width, height = guiGetScreenSize () 
local lx, ly, lz = getWorldFromScreenPosition ( width/2, height/2, 10 ) 
setPedLookAt(player, lx, ly, lz) 
end  
end 
    
end) 
  

Link to comment
Something like this?:

Server:

function rotateHead() 
    for i,v in ipairs(getElementsByType("player")) do 
        local _,_,_, x,y,z = getCameraMatrix(v) 
        triggerClientEvent("rotateHead", v, x, y, z) 
    end 
end 
setTimer(rotateHead, 3000, 0) 

Client:

addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(rx, ry, rz) 
    setPedLookAt (source, rx, ry, rz, -1) 
end) 

It says in debugscript 3: Bad Argument @ 'setPedLookAt'

a Timer? are you crazy?

Link to comment
Something like this?:

Server:

function rotateHead() 
    for i,v in ipairs(getElementsByType("player")) do 
        local _,_,_, x,y,z = getCameraMatrix(v) 
        triggerClientEvent("rotateHead", v, x, y, z) 
    end 
end 
setTimer(rotateHead, 3000, 0) 

Client:

addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(rx, ry, rz) 
    setPedLookAt (source, rx, ry, rz, -1) 
end) 

It says in debugscript 3: Bad Argument @ 'setPedLookAt'

What I mean is that you triggered ('rotateHead', v, x, y, z) then you wanted to set the ped looking at rx, ry, rz, which is not defined.

Link to comment
  • Moderators

ignore pa3ck his post, it seems he doesn't know what the source is....

Btw try this: (not tested)

It will give you better information about the head rotation, the default update of the camera matrix is 4 secs.

addEvent("sendRotateHead",true) 
addEventHandler("sendRotateHead",root, 
function (xr,yr,zr) 
    if isElement(source) then -- make sure he is still in game 
        triggerClientEvent(root,"rotateHead",source,xr,yr,zr) 
    end 
end) 

  
  
  
  
local headUpdateFunction = function () 
    local x,y,z,xr,yr,zr = getCameraMatrix(localPlayer) 
    if xr and getElementHealth(localPlayer) > 0 then 
        triggerServerEvent("sendRotateHead", localPlayer, xr,yr,zr) 
    end 
end 
  
addEventHandler("onClientResourceStart",root, 
function () 
    setTimer(headUpdateFunction,1000,0) 
end) 
  
  
addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(xr,yr,zr) 
    if isElement(source then -- make sure he is still in game 
        setPedLookAt (source, xr,yr,zr, 100) 
    end 
end) 

Updated.

Link to comment

idk if I am late try this :)

function movePlayerHead() 
    for k, players in ipairs(getElementsByType("player")) do 
        if (getElementHealth(players) > 1) then 
            local w, h = guiGetScreenSize() 
            local lookatX, lookatY, lookatZ = getWorldFromScreenPosition(w/2, h/2, 10) 
            setPedLookAt(players, lookatX, lookatY, lookatZ) 
        end 
    end 
end  

:P lol after reading this topic I saw that I use the same as wasim's one :)

Link to comment
ignore pa3ck his post, it seems he doesn't know what the source is....

Btw try this: (not tested)

It will give you better information about the head rotation, the default update of the camera matrix is 4 secs.

addEvent("sendRotateHead",true) 
addEventHandler("sendRotateHead",root, 
function (xr,yr,zr) 
    if isElement(source) then -- make sure he is still in game 
        triggerClientEvent(root,"rotateHead",source,xr,yr,zr) 
    end 
end) 

  
  
  
  
  
local headUpdateFunction = function () 
    local x,y,z,xr,yr,zr = getCameraMatrix(localPlayer) 
    if xr and getElementHealth(localPlayer) > 0 then 
        triggerServerEvent("sendRotateHead", localPlayer, xr,yr,zr) 
    end 
end 
  
addEventHandler("onClientResourceStart",root, 
function () 
    setTimer(headUpdateFunction,1000,0) 
end) 
  
  
addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(xr,yr,zr) 
    if isElement(source then -- make sure he is still in game 
        setPedLookAt (source, xr,yr,zr, 100) 
    end 
end) 

Updated.

Now, the character is looking at me(camera).

Something like this?:

Server:

function rotateHead() 
    for i,v in ipairs(getElementsByType("player")) do 
        local _,_,_, x,y,z = getCameraMatrix(v) 
        triggerClientEvent("rotateHead", v, x, y, z) 
    end 
end 
setTimer(rotateHead, 3000, 0) 

Client:

addEvent("rotateHead", true) 
addEventHandler("rotateHead", root, 
function(rx, ry, rz) 
    setPedLookAt (source, rx, ry, rz, -1) 
end) 

It says in debugscript 3: Bad Argument @ 'setPedLookAt'

a Timer? are you crazy?

Have you got any good idea?

Link to comment
  • 1 month later...
  • Moderators

For what I created:

"onClientResourceStart" 
"onClientRender" 
"onPlayerSpawn" 

triggerClientEvent 
table = {} 
getNetworkStats 
getDistanceBetweenPoints3D  
getTickCount 

and much more...

for a working a script, that's keeping up the performance of your internet speed.

At the moment I am busy with something similar to combination of slothbot and dayZ, I don't have time to guide you, sorry.

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