Xwad Posted July 26, 2016 Share Posted July 26, 2016 How can i make the vehicle going backwards? Because the setElementVelocity does not move it backwards, it always moves the vehicle direction south:/ function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local vx,vy,vz = getElementVelocity(veh) --local rx,ry,rz = getElementRotation(veh) --vx-rx = x --vy-ry = y setElementVelocity(veh, vx-0.1,vy-0.1,vz) end end end bindKey("s", "down", backwards) Link to comment
novo Posted July 26, 2016 Share Posted July 26, 2016 local vx, vy, vz = getElementVelocity(veh) setElementVelocity( veh, -vx, -vy, vz ) Link to comment
Xwad Posted July 26, 2016 Author Share Posted July 26, 2016 Its not good:/ its only working if i go forwards with the vehicle Link to comment
undefined Posted July 26, 2016 Share Posted July 26, 2016 function setSpeed(vehicle, velocity, vz) local _,_,rz = getElementRotation(vehicle) rz = rz - math.floor(math.fmod(rz, 90)) * 90 local rad = math.rad(90 - rz) vx, vy = math.cos(rad) * velocity, math.sin(rad) * velocity setElementVelocity(vehicle, vx, vy, vz or 0) end function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local vx,vy,vz = getElementVelocity(veh) local speed = vx + vy setSpeed(veh, speed - 0.2, vz) end end end bindKey("s", "down", backwards) Try it (not tested) Link to comment
Xwad Posted July 26, 2016 Author Share Posted July 26, 2016 line 8 bad argumentum setElementVelocity expected number at argumentum 4, got nil Link to comment
Xwad Posted July 26, 2016 Author Share Posted July 26, 2016 its not working correctly:/ its going all direction, one time its moving left, other time right, or forward:/ Link to comment
undefined Posted July 26, 2016 Share Posted July 26, 2016 function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local vx,vy,vz = getElementVelocity(veh) local rate, speed = vx/vy, 0.1 setElementVelocity(veh, vx - speed * rate, vy - speed, vz) end end end bindKey("s", "down", backwards) Link to comment
Xwad Posted July 26, 2016 Author Share Posted July 26, 2016 line 8 bad argumentum setElementVelocity expected number got, NaN Link to comment
undefined Posted July 26, 2016 Share Posted July 26, 2016 line 8 bad argumentum setElementVelocity expected number got, NaN local rate, speed = vy ~= 0 and vx/vy or 1, 0.1 But this code only work if the vehicle is moving. ------------- Try new code. (Not tested) function setSpeed(vehicle, velocity, vz) local _,_,rz = getElementRotation(vehicle) local aspect = math.floor(math.fmod(rz, 90)) * 90 rz = rz - aspect local rad = math.rad(aspect - rz) vx, vy = math.cos(rad) * velocity, math.sin(rad) * velocity setElementVelocity(vehicle, vx, vy, vz or 0) end function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local vx,vy,vz = getElementVelocity(veh) local speed = vx + vy setSpeed(veh, speed - 0.2, vz) end end end bindKey("s", "down", backwards) Link to comment
Xwad Posted July 26, 2016 Author Share Posted July 26, 2016 going to all directins again:/ Link to comment
Wojak Posted July 27, 2016 Share Posted July 27, 2016 function getPositionFromElementOffset(element,offX,offY,offZ) local m = getElementMatrix ( element ) local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1] local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2] local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3] return x, y, z end function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local vx,vy,vz = getElementVelocity(veh) local speed = (vx^2 + vy^2 + vz^2)^0.5 vx,vy,vz = getPositionFromElementOffset(veh,0,-speed,0) setElementVelocity(veh,vx,vy,vz) end end end bindKey("s", "down", backwards) Link to comment
Xwad Posted July 27, 2016 Author Share Posted July 27, 2016 well, now its really interesting. When i press s the on my screen a loading windows hops up, but after that nothing happens Link to comment
Wojak Posted July 27, 2016 Share Posted July 27, 2016 woops... function getPositionFromElementOffset(element,offX,offY,offZ) local m = getElementMatrix ( element ) local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1] local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2] local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3] return x, y, z end function backwards() if isPedInVehicle (localPlayer) then local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 498 then toggleControl ( "brake_reverse", false ) local x,y,z = getElementPosition(veh) local vx,vy,vz = getElementVelocity(veh) local speed = (vx^2 + vy^2 + vz^2)^0.5 vx,vy,vz = getPositionFromElementOffset(veh,0,-speed,0) setElementVelocity(veh,vx-x,vy-y,vz-z) end end end bindKey("s", "down", backwards) Link to comment
Xwad Posted July 27, 2016 Author Share Posted July 27, 2016 Only working if i go warwards:/ And i want to make that if i press s then it starting going backwards slowly. Its not possible to change the backwards velocity, that why i have to do it this way Link to comment
Wojak Posted July 27, 2016 Share Posted July 27, 2016 i want to make that if i press s then it starting going backwards slowly. This information should be in the opening post, it's your fault that this problem is not solved yet... delete line 14 change line 15 to local speed = 0.5 Predicting your future problems: - its to slow/fast – change 0.5 to different value - I need to tap s for it to work – use onClientRender or setTimer + detecting if the key is pressed - I want it to accelerate – use math to change the value of speed based on the current conditions good luck. Link to comment
Xwad Posted July 27, 2016 Author Share Posted July 27, 2016 Now its working Thanks a lot guys!!! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now