Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. local gMe = getLocalPlayer()
  2. local gLastTick = nil
  3.  
  4. local TUBE_RADIUS = 5
  5. local TOWARD_LINE_FACTOR = 0.001
  6. local ALONG_LINE_FACTOR = 0.0003
  7.  
  8. local gLines = {
  9.     { vec(0, 0, 3), vec(0, 10, 10) }
  10. }
  11.  
  12. addEventHandler('onClientResourceStart', resourceRoot,
  13.     function()
  14.        
  15.         for i,line in ipairs(gLines) do
  16.             line.length = (line[2] - line[1]):len()
  17.             line.dir = (line[2] - line[1]):normalize()
  18.         end
  19.     end
  20. )
  21.  
  22. addEventHandler('onClientRender', root,
  23.     function()
  24.         local vehicle = getPedOccupiedVehicle(gMe)
  25.         if not vehicle then
  26.             return
  27.         end
  28.        
  29.         local newTick = getTickCount()
  30.         if not gLastTick then
  31.             gLastTick = newTick - 50
  32.         end
  33.         local interval = newTick - gLastTick
  34.         gLastTick = newTick
  35.        
  36.         local vPos = vec(getElementPosition(vehicle))
  37.         for i,line in ipairs(gLines) do
  38.             dxDrawLine3D(line[1][1], line[1][2], line[1][3], line[2][1], line[2][2], line[2][3], tocolor(255, 0, 0))
  39.            
  40.             local distAlongLine = (vPos - line[1]) * line.dir
  41.             if distAlongLine >= 0 and distAlongLine <= line.length then
  42.                 local carToLine = line.dir:cross(line.dir:cross(vPos - line[1]))
  43.                 local distToLine = carToLine:len()
  44.                 if distToLine <= TUBE_RADIUS then
  45.                     local vVel = vec(getElementVelocity(vehicle))
  46.                     vVel = vVel + (carToLine / distToLine) * (distToLine / TUBE_RADIUS)*TOWARD_LINE_FACTOR*interval
  47.                     vVel = vVel + line.dir * ALONG_LINE_FACTOR*interval
  48.                     setElementVelocity(vehicle, unpack(vVel))
  49.                     break
  50.                 end
  51.             end
  52.         end
  53.     end
  54. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement