Sign in to follow this  
Xwad

setElementVelocity backwards

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) 

Share this post


Link to post
local vx, vy, vz = getElementVelocity(veh) 
setElementVelocity( 
    veh, 
    -vx, 
    -vy, 
    vz   
) 

Share this post


Link to post

Its not good:/ its only working if i go forwards with the vehicle :/

Share this post


Link to post
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)

Share this post


Link to post

line 8 bad argumentum setElementVelocity expected number at argumentum 4, got nil

Share this post


Link to post

its not working correctly:/ its going all direction, one time its moving left, other time right, or forward:/

Share this post


Link to post
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) 

Share this post


Link to post

line 8 bad argumentum setElementVelocity expected number got, NaN

Share this post


Link to post
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) 

Share this post


Link to post
  
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) 
  

Share this post


Link to post

well, now its really interesting. When i press s the on my screen a loading windows hops up, but after that nothing happens

Share this post


Link to post

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) 

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.