Jump to content

Laser


DarkByte

Recommended Posts

Posted

Hi i tried to make a script and its not working. I wanna add laser at the tank.

function laser ( creator) 
        if getElementType(creator) == "vehicle" then 
        if getElementModel(creator) == 432 then 
        local x, y = getVehicleComponentPosition( creator, "misc_c" ) 
        local x1, y1 = getVehicleComponentPosition( creator, "misc_a" ) 
        dxDrawLine ( x, y, x1, y1, tocolor ( 255, 0, 0, 255 ) 
    end 
end 
end 
end 
addEventHandler("onClientVehicleEnter", getRootElement(), laser) 

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

Posted

yes ofc it will not work since you didn't add the event "onClientRender" because it's very important for dx functions

so it should be like this

local x, y 
local x1, y1 
  
function laser () 
local theVeh = getPedOccupiedVehicle(localPlayer) 
        if getElementModel(theVeh) == 432 then 
        x, y = getVehicleComponentPosition( theVeh, "misc_c" ) 
        x1, y1 = getVehicleComponentPosition( theVeh, "misc_a" ) 
        addEventHandler("onClientRender",root,add) 
    end 
end 
addEventHandler("onClientVehicleEnter", getRootElement(), laser) 
  
function add() 
dxDrawLine ( x, y, x1, y1, tocolor ( 255, 0, 0, 255 ) ) 
end 
  

and what the hell is creator xD

Posted

You want it to look like the picture? And how does it look right now? Are you sure the components are correct?

  • Moderators
Posted

Get the function extendLine from the stealth resource, to extend the line...

Which you probably can find in the main client file from stealth.

And don't forget that the z variable is required from getVehicleComponentPosition to make this work.

Posted

I found it. Can you help me futher.

local laserWeapons = {} 
function drawLasers() 
    for k,player in ipairs(getElementsByType"player") do 
        local playerWeapon = getPedWeapon ( player ) 
        if ( laserWeapons[playerWeapon] ) then 
            local startX,startY,startZ,targetX,targetY,targetZ 
            local boneX,boneY,boneZ = getPedBonePosition ( player, 25 ) 
            startX,startY,startZ = getPedWeaponMuzzlePosition ( player ) 
            if boneX and startX then 
                if getPedControlState(player, "aim_weapon") then 
                    targetX, targetY, targetZ = getPedTargetEnd(player) 
                else 
                    targetX,targetY,targetZ = extendLine ( boneX,boneY,boneZ,startX,startY,startZ - 0.1,500 ) 
                end 
                 
                local bool,hitX,hitY,hitZ = processLineOfSight ( startX,startY,startZ,targetX,targetY,targetZ, true, true, true, true, true, false, false, true ) 
                if not bool or not hitX then 
                    hitX,hitY,hitZ = targetX,targetY,targetZ 
                end 
                dxDrawLine3D ( startX,startY,startZ, hitX,hitY,hitZ, tocolor(255,0,0,50), 1, false, 1 ) 
            end 
        end 
    end 
end 

  • Moderators
Posted

In the code you posted there is only a reference to that function.

You have to get the full function.

function extendLine ( .... 
... 
.. 
.. 
end 

Posted
function extendLine ( x,y,z,x2,y2,z2,length ) 
    local vx = x2 - x 
    local vy = y2 - y 
    local vz = z2 - z 
    local ratio = length/(getDistanceBetweenPoints3D ( x,y,z,x2,y2,z2 )) 
    vx = vx*ratio 
    vy = vy*ratio 
    vz = vz*ratio 
    return (x + vx),(y + vy),(z + vz) 
end 

  • Moderators
Posted

It is not really optimised based on when it renders. But it should be working. I added dxDrawLine3D instead of dxDrawLine because it is easier to use for 3D. But if you want to keep the line the same thickness, you will have to study this function: https://wiki.multitheftauto.com/wiki/Ge ... ldPosition in order to use dxDrawLine > 3D.

    function extendLine ( x,y,z,x2,y2,z2,length ) 
        local vx = x2 - x 
        local vy = y2 - y 
        local vz = z2 - z 
        local ratio = length/(getDistanceBetweenPoints3D ( x,y,z,x2,y2,z2 )) 
        vx = vx*ratio 
        vy = vy*ratio 
        vz = vz*ratio 
        return (x + vx),(y + vy),(z + vz) 
    end 

local renderTankLine = function () 
    local theVeh = getPedOccupiedVehicle(localPlayer) 
    if theVeh and getElementModel(theVeh) == 432 then 
        local x,y,z = getVehicleComponentPosition( theVeh, "misc_c" ) 
        local x1,y1,z1 = getVehicleComponentPosition( theVeh, "misc_a" ) 
        if x and x1 then 
            local endX,endY,endZ = extendLine(x,y,z,x1,y1,z1,300) 
            dxDrawLine3D (x,y,z,endX,endY,endZ,tocolor(255,0,0)) 
        end 
    end 
end 
addEventHandler("onClientRender",root,renderTankLine) 

Update 19:35

Posted
function renderTankLine() 
    local theVeh = getPedOccupiedVehicle(localPlayer) 
    if theVeh and getElementModel(theVeh) == 432 then 
        local x,y,z = getVehicleComponentPosition( theVeh, "misc_c" ) 
        local x1,y1,z1 = getVehicleComponentPosition( theVeh, "misc_a" ) 
        if x and x1 then 
            local endX,endY,endZ = extendLine(x,y,z,x1,y1,z1,300) 
            dxDrawLine3D (x,y,z,endX,endY,endZ,tocolor(255,0,0)) 
        end 
    end 
end 
addEventHandler("onClientRender",root,renderTankLine) 

  • Moderators
Posted

That warning you see is not from me.

@HUNGRY:3 what did you change? I don't see any differences.

@-Lemonade- Try to make the line a little bit thicker, because it must be somewhere.

Posted
That warning you see is not from me.

@HUNGRY:3 what did you change? I don't see any differences.

@-Lemonade- Try to make the line a little bit thicker, because it must be somewhere.

Line 1 you did

local renderblalala = function()

It's from you :- ) IIYAMA-SENPAI

That's why it's give error addEventHandler expected function

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