Jump to content

setElementVelocity backwards


Xwad

Recommended Posts

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

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

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