Jump to content

Problem with reverse lights script for vehicles


ILMC

Recommended Posts

Posted

Hi, I wanted to make reverse lights for cars on my server and found a suitable script from Lord Henry on the Wiki. https://wiki.multitheftauto.com/wiki/IsVehicleReversing I've already made lights for many IDs, but it turns out that the lights only work properly within a radius of about 300 meters from the car's spawn point. At greater distances, the lights start to turn on for a short period of time (specified in the timer at the end of the code), then turn off, and after about a second, turn on again and turn off. How can I solve this problem? Lord Henry said that when he made this script a long time ago, everything worked fine. Perhaps it depends on the MTA version, or he didn't quite understand the problem. It makes no difference whether the script is running on the server or the client. The server.lua code, which is almost identical to the standard one and works the same way:

function isVehicleReversing(theVehicle)
    local getMatrix = getElementMatrix (theVehicle)
    local getVelocity = Vector3 (getElementVelocity(theVehicle))
    local getVectorDirection = (getVelocity.x * getMatrix[2][1]) + (getVelocity.y * getMatrix[2][2]) + (getVelocity.z * getMatrix[2][3])
    if (getVectorDirection < 0) then
        return true
    end
    return false
end

rearLightL = {}
rearLightR = {}

function setVehicleRearLightState566 (theVehicle, light, state)
    local x, y, z = getElementPosition (theVehicle)
    if getElementModel (theVehicle) == 566 then 
        if light == 0 then
            if state == 0 then
                if not rearLightL[theVehicle] then
                    rearLightL[theVehicle] = createMarker (x, y, z, "corona", 0.1, 255, 255, 220, 200)
                end
                setMarkerSize (rearLightL[theVehicle], 0.12)
                attachElements (rearLightL[theVehicle], theVehicle, -0.45, -2.69, 0.01)
            elseif state == 1 then
                if rearLightL[theVehicle] then
                    setMarkerSize (rearLightL[theVehicle], 0)
                end
            elseif state == 2 then
                if rearLightL[theVehicle] then
                    destroyElement (rearLightL[theVehicle])
                    rearLightL[theVehicle] = nil
                end
            end
        elseif light == 1 then
            if state == 0 then
                if not rearLightR[theVehicle] then
                    rearLightR[theVehicle] = createMarker (x, y, z, "corona", 0.1, 255, 255, 220, 200)
                end
                setMarkerSize (rearLightR[theVehicle], 0.12)
                attachElements (rearLightR[theVehicle], theVehicle, 0.45, -2.69, 0.01)
            elseif state == 1 then
                if rearLightR[theVehicle] then
                    setMarkerSize (rearLightR[theVehicle], 0)
                end
            elseif state == 2 then
                if rearLightR[theVehicle] then
                    destroyElement (rearLightR[theVehicle])
                    rearLightR[theVehicle] = nil
                end
            end
        else
            return false
        end
    end
end

function isRearing ()
    local everyone = getElementsByType ("player")
    for i,v in ipairs (everyone) do
        local vehicle = getPedOccupiedVehicle (v)
        if vehicle then
            if isVehicleReversing(vehicle) then
                setVehicleRearLightState566 (vehicle, 0, 0) -- Turns the 'back light' on.
                setVehicleRearLightState566 (vehicle, 1, 0)
            else
                setVehicleRearLightState566 (vehicle, 0, 1) -- Turns the 'back light' off.
                setVehicleRearLightState566 (vehicle, 1, 1)
            end
        end
    end
end
setTimer (isRearing, 100, 0)

There's a similar script that creates flashing lights, and I sometimes use it as hazard lights. https://forum.multitheftauto.com/topic/37967-rel-emerlights-10-emergency-lights/ It works across the entire map in main dimension. Both scripts create markers in coordinates specified in the script and don't require model modifications.

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