Jump to content

Rotating object with camera move


Xwad

Recommended Posts

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
Link to comment

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.

Link to comment

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

Link to comment

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

Link to comment
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

Link to comment

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

Link to comment

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

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