Jump to content

[Help] Ped Rotation ; setElementVelocity.


Perfect

Recommended Posts

Hello, I am trying to, Rotate Ped at a specified region in circles.

I have been trying this from last 1 hour, continuously. Well, here is Script :-

myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
k = 15 
  
function pedRotate() 
if k <= 360 then 
local rx,ry,rz = getElementRotation(myPed) 
setElementRotation(myPed,rx,ry,rz+k) 
setElementVelocity(myPed,1,1,1) 
k = k + 15 
else  
k = 15 
end 
end 
  
addEventHandler( "onClientResourceStart", getRootElement(), 
function() 
setTimer(pedRotate,500,0) 
end) 

Can Someone Please Solve This ?

Edited by Guest
Link to comment
  • Moderators
  
myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
local k = 0 
  
function pedRotate() 
    if k+15 <= 360 then 
        --setElementVelocity(myPed,1,1,1) 
        k = k + 15 
    else 
        k = 0 
    end 
    local rx,ry,rz = getElementRotation(myPed) 
    setElementRotation(myPed,rx,ry,k) 
end 
  
addEventHandler( "onClientResourceStart", getRootElement(), 
function() 
    setTimer(pedRotate,500,0) 
end) 
  
  

Link to comment
  
myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
local k = 0 
  
function pedRotate() 
    if k+15 <= 360 then 
        --setElementVelocity(myPed,1,1,1) 
        k = k + 15 
    else 
        k = 0 
    end 
    local rx,ry,rz = getElementRotation(myPed) 
    setElementRotation(myPed,rx,ry,k) 
end 
  
addEventHandler( "onClientResourceStart", getRootElement(), 
function() 
    setTimer(pedRotate,500,0) 
end) 
  
  

Thanks for trying to help but Its not working.

Ped is Rotating but in zip zap pattern. I want it to rotate in circular motion in same place.

Can You Please Solve This ?

Here is full script:-

myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
local k = 0 
  
function pedRotate() 
    if k+15 <= 360 then 
        k = k + 15 
    else 
        k = 0 
    end 
    local rx,ry,rz = getElementRotation(myPed) 
    setElementRotation(myPed,rx,ry,k) 
    setPedControlState(myPed,"forwards",true) 
end 
  
addEventHandler( "onClientResourceStart", getRootElement(), 
function() 
    setTimer(pedRotate,500,0) 
end) 

Link to comment
  • Moderators

Next time explain better. I am still not sure what you mean.

myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
setPedControlState(myPed,"forwards",true) 
  
local rotationSpeed = 0.04 
function pedRotate() 
    local rx,ry,rz = getElementRotation(myPed) 
    --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
    rz = rz+rotationSpeed  
    if rz <= 360 then 
setPedCameraRotation (ped,-rz) 
    else 
setPedCameraRotation (ped,0) 
    end 
end 
  
addEventHandler( "onClientResourceStart", root, 
function() 
    addEventHandler("onClientRender",root,pedRotate) 
end) 

Link to comment
Next time explain better. I am still not sure what you mean.
myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
setPedControlState(myPed,"forwards",true) 
  
local rotationSpeed = 0.04 
function pedRotate() 
    local rx,ry,rz = getElementRotation(myPed) 
    --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
    rz = rz+rotationSpeed  
    if rz <= 360 then 
setPedCameraRotation (ped,-rz) 
    else 
setPedCameraRotation (ped,0) 
    end 
end 
  
addEventHandler( "onClientResourceStart", root, 
function() 
    addEventHandler("onClientRender",root,pedRotate) 
end) 

Well, Sorry for not Explaining clear. (Iam really bad at explaining somethings sometimes)

I am trying to create a ped at a Place, Which will walk 2 meters then rotate then walk 2 meter then rotate .... in circular motion.

unfortunately, The Script which you provide is not working, The ped is just moving forward but not rotating.

Link to comment
  • Moderators

AFAIK you also can see that I wrote a wrong variable. Always check people their code, before testing it.

Also check your debugscript(/debugscript 3) then you would also know why it isn't working.

Try this:

    myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
    setElementRotation(myPed,0,0,0) 
    setPedControlState(myPed,"forwards",true) 
      
    local rotationSpeed = 15 
    function pedRotate() 
        local rx,ry,rz = getElementRotation(myPed) 
        --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
        rz = rz+rotationSpeed 
        if rz <= 360 then 
            setPedCameraRotation (myPed,-rz) 
        else 
            setPedCameraRotation (myPed,0) 
        end 
    end 
      
    addEventHandler( "onClientResourceStart", root, 
    function() 
        setTimer(pedRotate,500,0) 
    end) 

Link to comment
AFAIK you also can see that I wrote a wrong variable. Always check people their code, before testing it.

Also check your debugscript(/debugscript 3) then you would also know why it isn't working.

Try this:

    myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
    setElementRotation(myPed,0,0,0) 
    setPedControlState(myPed,"forwards",true) 
      
    local rotationSpeed = 15 
    function pedRotate() 
        local rx,ry,rz = getElementRotation(myPed) 
        --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
        rz = rz+rotationSpeed 
        if rz <= 360 then 
            setPedCameraRotation (myPed,-rz) 
        else 
            setPedCameraRotation (myPed,0) 
        end 
    end 
      
    addEventHandler( "onClientResourceStart", root, 
    function() 
        setTimer(pedRotate,500,0) 
    end) 

Well, i change 'ped' to 'myPed' then i use debugscript 3 in-game. it tells me that there is a bad argument at setPedCameraRotation, So i changed '-rz' to 'rz - (rz * 2' (which is same).

Thanks for Trying to help :)

But still script is not working, Ped is just moving forward but not rotating.

(for info, it is client side as setPedControlState is)

Link to comment
  • Moderators
  
myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
setPedControlState(myPed,"forwards",true) 
  
local rotationSpeed = 45 
function pedRotate() 
    local rx,ry,rz = getElementRotation(myPed) 
    --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
    rz = rz+rotationSpeed 
    if rz <= 360 then 
        setPedCameraRotation (myPed,-rz) 
    else 
        setPedCameraRotation (myPed,0) 
    end 
end 
  
addEventHandler( "onClientResourceStart",resourceRoot, 
function() 
    setTimer(pedRotate,500,0) 
end) 
  
  

Works perfectly.

Link to comment
  
myPed = createPed(23,-1375.30627, -234.72827, 14.14844) 
setElementRotation(myPed,0,0,0) 
setPedControlState(myPed,"forwards",true) 
  
local rotationSpeed = 45 
function pedRotate() 
    local rx,ry,rz = getElementRotation(myPed) 
    --setElementPosition (myPed, -1375.30627, -234.72827, 14.14844, false) 
    rz = rz+rotationSpeed 
    if rz <= 360 then 
        setPedCameraRotation (myPed,-rz) 
    else 
        setPedCameraRotation (myPed,0) 
    end 
end 
  
addEventHandler( "onClientResourceStart",resourceRoot, 
function() 
    setTimer(pedRotate,500,0) 
end) 
  
  

Works perfectly.

Well, If you wait there(where ped was rotating) for few minutes, you will know Its not perfect.

The Ped is Rotating but also moving like he was moving northwards with moving in circles.

I want it to rotate in one place.

Thank You So Much so far. :D

Link to comment

Try.

myPed = createPed(23, -1375.30627, -234.72827, 14.14844) 
setPedControlState(myPed, "forwards", true) 
  
addEventHandler( "onClientRender", root, 
function() 
    angleCounter = (angleCounter or 0) + 5 
    if angleCounter >= 360 then 
        angleCounter = 0 
    end 
    setPedCameraRotation(myPed, angleCounter) 
end) 

Link to comment
  • Moderators

Well if you want that, you have to use trigonometric functions/math to create points around your main point.

To create a kind of path or a dynamic path.

Except I am not going to burn my fingers on that.

Edited by Guest
Link to comment
Try.
myPed = createPed(23, -1375.30627, -234.72827, 14.14844) 
setPedControlState(myPed, "forwards", true) 
  
addEventHandler( "onClientRender", root, 
function() 
    angleCounter = (angleCounter or 0) + 5 
    if angleCounter >= 360 then 
        angleCounter = 0 
    end 
    setPedCameraRotation(myPed, angleCounter) 
end) 

Well, I try your script, its better than the previous one. but ped is moving 1 step to southwards after 1-2 rounds of ratation.

As i said, i want it to rotate same place. so after many hours, it should be rotating in same place without moving inch from the circle.

Well if you want that, you have to use trigonometric functions/math to create points around your main point.

To create a kind of path.

Except I am not going to burn my fingers on that.

Well, You already tried so much for me. Thank you for that.

But can you tell me exactly what i should do, I am very weak in trigonometry and even more weak when trigonometry come to scripting. Can you please give me example of what i should do.(please wright steps in simple and plain english)

Link to comment

I was feeling bored so i played with the code a little bit, as I don't know much about math so i took Socialz code from here dxDrawPartialCircle and turned it into this magic code. :mrgreen:

Try it.

myPed = createPed(23, -1375.30627, -234.72827, 14.14844) 
setPedControlState(myPed, "forwards", true) 
  
radius = 50 -- doesn't seem to add any good effect 
width = 5 -- doesn't seem to add any good effect 
per = 3 -- lower this to increase the circle 
  
addEventHandler("onClientRender", root, 
function() 
    percentage = percentage and percentage or 0 
    if (percentage < 360) then  
        percentage = percentage+per 
    else 
        percentage = 0 
    end 
     
    local function clamp(val, lower, upper) 
        if (lower > upper) then lower, upper = upper, lower end 
        return math.max(lower, math.min(upper, val)) 
    end 
  
    startAngle = clamp(0, 0, 360) 
    endAngle = clamp(percentage or 360, 0, 360) 
     
    if (endAngle < startAngle) then 
        endaAngle = startAngle 
        startAngle = endAngle 
    end 
     
    local posX, posY = getElementPosition(myPed) 
    for i=startAngle,endAngle do 
        local _i = i*(math.pi/180) 
        setPedRotation(myPed, findRotation(math.cos(_i)*(radius-width)+posX, math.sin(_i)*(radius-width)+posY, -1375.30627, -234.72827)) 
    end 
end) 
  
 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 

Link to comment
I was feeling bored so i played with the code a little bit, as I don't know much about math so i took Socialz code from here dxDrawPartialCircle and turned it into this magic code. :mrgreen:

Try it.

myPed = createPed(23, -1375.30627, -234.72827, 14.14844) 
setPedControlState(myPed, "forwards", true) 
  
radius = 50 -- doesn't seem to add any good effect 
width = 5 -- doesn't seem to add any good effect 
per = 3 -- lower this to increase the circle 
  
addEventHandler("onClientRender", root, 
function() 
    percentage = percentage and percentage or 0 
    if (percentage < 360) then  
        percentage = percentage+per 
    else 
        percentage = 0 
    end 
     
    local function clamp(val, lower, upper) 
        if (lower > upper) then lower, upper = upper, lower end 
        return math.max(lower, math.min(upper, val)) 
    end 
  
    startAngle = clamp(0, 0, 360) 
    endAngle = clamp(percentage or 360, 0, 360) 
     
    if (endAngle < startAngle) then 
        endaAngle = startAngle 
        startAngle = endAngle 
    end 
     
    local posX, posY = getElementPosition(myPed) 
    for i=startAngle,endAngle do 
        local _i = i*(math.pi/180) 
        setPedRotation(myPed, findRotation(math.cos(_i)*(radius-width)+posX, math.sin(_i)*(radius-width)+posY, -1375.30627, -234.72827)) 
    end 
end) 
  
 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 

That Magic code is same as previous ones.

The Ped is rotating in circle but the circle's size is increasing and decreasing ( or ped is moving in different directions with rotating in circle)

Link to comment
  • Moderators

Try to change it to that:

setPedCameraRotation(myPed, -findRotation(math.cos(_i)*(radius-width)+posX, math.sin(_i)*(radius-width)+posY, -1375.30627, -234.72827)) 

Always remember that the ped rotation is the opposite of the camera rotation. So that's why I did: -

Link to comment
Try to change it to that:
setPedCameraRotation(myPed, -findRotation(math.cos(_i)*(radius-width)+posX, math.sin(_i)*(radius-width)+posY, -1375.30627, -234.72827)) 

Always remember that the ped rotation is the opposite of the camera rotation. So that's why I did: -

Oh, Thank you for the advice. But Script is still same.

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