Jump to content

Rotating object with camera move


Xwad

Recommended Posts

Posted (edited)

Hi. I made a script that makes possible if i rotate the players camera to the right then the object will also rotate right. I mean that the object is rotating to that direction , where the players camera is looking. The only problem is that its not working..

Debugscript:

lua:7: Bad Argumentum @ 'getElementRotation' [Expected element at argumentum 1,got nil]

lua:8: Bad Argumentum @ 'getElementPosition' [Expected element at argumentum 1,got nil]

lua:10: attempt to call global 'findRotation' (a nil value)

Client.lua

  
local test = createWeapon("m4", -2388.41, -581.15, 132.3) 
setWeaponState(test, "ready") 
     
  
  
function rotate ()  
  local rotX, rotY, rotZ = getElementRotation(source)  
  local x, y, z = getElementPosition(source) 
  local rotX, rotY, rotZ = getElementRotation(test)  
  setElementRotation(test, findRotation(x,y,rotX,rotY) )     
end 
  
function bindTheKeys () 
  bindKey ( "h", "down", rotate )   
end 
addEventHandler("onClientResourceStart", getRootElement(), bindTheKeys) 
  

Edited by Guest
Posted

First of all the variable test should not be local as you've called it in the function.

Secondly the source you've mentioned might not be the player maybe use thePlayer.

Sorry am not a computer can't check the code....

Posted

Variable test can be local, it's in the same file so that's not the problem. The function however is called by a keybind you set during resource start (noticed this is a client side resource) so you should use localPlayer instead of source. Your first two errors are caused by the fact that there is no source defined in that function.

Moving on to the third error which is caused by an undefined function called "findRotation", you may have spelled wrong or forgot to define it, in any case there is no such function built in.

Posted

I changed the source to localPlayer. Its good now , but yeah theres still the error with the findRotation.. So its not defined? thats why its not working? So what should i do to fix that problem? Thanks.

Posted

Add this to your script.

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

And findRotation takes positions of elements as arguments.

Posted

Thanks Army@1!! Its still not working but theres no more error with setElementRotation, only a bad argumentum: lua:10: Bad Argumentum @ 'setElementRotation' [Expected number at argumentum 3, got none]

Whats could be the problem now?

Posted

The problem here is that the function "findRotation" only returns the rotation in z axis while setElementRotation needs a vector with x,y and z, the solution is simple however. Change line 11 from this:

setElementRotation(test, findRotation(x,y,rotX,rotY))    

to this:

setElementRotation(test, 0,0, findRotation(x,y,rotX,rotY)) 

Posted

Now theres no error but its not working correctly:// The weapon is not rotating to the direction where the camera is looking

Posted

Try replacing this line

setElementRotation(test, 0,0, findRotation(x,y,rotX,rotY)) 

with this

setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) 
--or if the above doesn't work 
setElementRotation(test, 0, 0, getPedCameraRotation(localPlayer)) 

Posted
setElementRotation(test, 0, 0, getPedCameraRotation(localPlayer)) 

Actualy its working but not correctly again:/ When i use this one then the weapon is always rotating to the opposite side.. I mean if i look with the camera to the right side then the weapon will rotate to the left side..

:setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) 

This one is rotating correctly but this has also a problem.. when i put this line to a function and start the function the then weapon is looking to the right automaticly and i cant rotate it..Please help

Posted

sry sry sry its working i just changed the values now!! now its not moving to the left!!

i changed this

from this :

setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) 

to this:

setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) 

Thanks man that you are helping me im really grateful!!

But i have one more problem!! When i look up and down with the camera then the weapon is not rotating up and down!!

Posted

Where did you get this from?

setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) 

That looks weird to me.

Anyway, try this

setElementRotation(test, 180 - getPedCameraRotation(localPlayer), 180 - getPedCameraRotation(localPlayer), 180 - getPedCameraRotation(localPlayer)) 

Posted

its not working correctly. the weapon is fully rotated now and the rotating is always the opposet xD but wait i test it again

Posted

Why is it not rotating up and down?:/ pls help

  
setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) 
  

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