Jump to content

Problem with getting speed from target element.


Michcio

Recommended Posts

My script doesn't work. I can't find mistake in it.

local scx,scy = guiGetScreenSize() 
  
function suszara () 
local target = getPedTarget(getLocalPlayer()) 
local px,py,pz=getElementPosition(getLocalPlayer()) 
    if getPlayerTeam(getLocalPlayer()) == getTeamFromName("Policja") then 
        if target and getElementType(target) == "player" then 
            if target ~= getLocalPlayer() then 
                local x,y,z = getElementPosition(target) 
                local car = getPedOccupiedVehicle( target ) 
                if car then 
                local speed = math.floor(getDistanceBetweenPoints3D(0,0,0,getElementVelocity(car)) * 100 * 1.61) 
                    if speed then 
                        if getVehicleController(car) == target then 
                        local dystans=getDistanceBetweenPoints3D(x,y,z,px,py,pz) 
                        local cx,cy,cz = getCameraMatrix() 
                            if dystans <= 60 then 
                                if isLineOfSightClear(cx,cy,cz,px,py,pz,true,false,false,true,false,false,false,getPedOccupiedVehicle(getLocalPlayer())) then 
                                    dxDrawText(getPlayerName(getVehicleController(car)).." : "..speed.." km/h", scx*(213/1280), scy*(842/1024), scx*(977/1280), scy*(913/1024), tocolor(255, 255, 255, 255), 1, "bankgothic", "center", "center", false, false, true, false, false) 
                                end 
                            end 
                        end 
                    end 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),suszara) 
  

Link to comment

getElementVelocity returns 3 values. You need to use Pythagoras theorem to calculate actual speed since the velocity is returned in x, y and z format so you need to calculate the length of these values (x*x + y*y + z*z) * 100 (or 160). Calling function like that (line 12) will work but the multiplication would only multiply z (the last value returned from getElementVelocity function). Also, is it possible to target yourself? (line 8 seems pointless).

Link to comment

Doesn't work. Problem is not in speed.

function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
  
local scx,scy = guiGetScreenSize() 
  
function suszara () 
local target = getPedTarget(getLocalPlayer()) 
local px,py,pz=getElementPosition(getLocalPlayer()) 
    if getPlayerTeam(getLocalPlayer()) == getTeamFromName("Policja") then 
        if target and getElementType(target) == "player" then 
            if target ~= getLocalPlayer() then 
                local x,y,z = getElementPosition(target) 
                local car = getPedOccupiedVehicle( target ) 
                if car then 
                local speed = getElementSpeed(car,"kph") 
                    if speed then 
                        if getVehicleController(car) == target then 
                        local dystans=getDistanceBetweenPoints3D(x,y,z,px,py,pz) 
                        local cx,cy,cz = getCameraMatrix() 
                            if dystans <= 60 then 
                                if isLineOfSightClear(cx,cy,cz,px,py,pz,true,false,false,true,false,false,false,getPedOccupiedVehicle(getLocalPlayer())) then 
                                    outputChatBox(speed) 
                                    dxDrawText(getPlayerName(getVehicleController(car)).." : "..speed.." km/h", scx*(213/1280), scy*(842/1024), scx*(977/1280), scy*(913/1024), tocolor(255, 255, 255, 255), 1, "bankgothic", "center", "center", false, false, true, false, false) 
                                end 
                            end 
                        end 
                    end 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),suszara) 
  

Link to comment
Problem is here:
local car = getPedOccupiedVehicle( target ) 

As far as I know, when you target at a vehicle getPedTarget will return vehicle (target = vehicle) so there is no point "getting occupied vehicle of vehicle".

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