Jump to content

[Help]Getting Vehicle Speed


Xierra

Recommended Posts

Hi guys! I want to make an optional command for my Alternate_hud again, it's called the Digital Speedometer.

So here, I have tried to work on the script, but it doesn't work and I didn't know how to get speed. Here's my try:

function getspeed()
 
        unit =  160.9 --km/h only
        vx,vy,vz = getElementVelocity( getPedOccupiedVehicle( getLocalPlayer() ) )  
return math.floor( ( vx ^ 2 + vy ^ 2 + vz ^ 2 ) ^ 0.5 * unit + 0.5 ) 	
end --This part thanks to karlis, just edited a bit.
 
function drawspeed()
 
dxDrawText("100" ,911.0,654.0,982.0,685.0,tocolor(255,255,255,255),1.0,"bankgothic","right","left",false,false,false) --Speed DX text, already tried it with tostring(getspeed), gives "Function: Address < No need to tell btw."
dxDrawText("Km/h",989.0,664.0,1019.0,680.0,tocolor(255,255,255,255),1.0,"default","top","left",false,false,false) -- Units, later I ask this one.
end
addEventHandler("onClientRender", getRootElement(), drawspeed)

Link to comment

Use tostring( getspeed() ) because tostring( getspeed ) will convert function to a string and return its address but you want to call the function and convert returned value to string.

Link to comment

Use bindKey to bind a key to some function which will change value of unit. Also, make another global variable which will be the text to show what units it is, something like this:

function toggleUnits( )
   unit = ( unit < 160 ) and 160 or 100;
   unitStr = ( unitStr == "Km/h" ) and "mph" or "Km/h";
end

Link to comment

I can't manage a correct way where to put that part of the script. So then how should I edit?

Script:

function getspeed()
 
        unit =  160.9 -- Km/h
 
 
        vx,vy,vz = getElementVelocity( getPedOccupiedVehicle( getLocalPlayer() ) )  
 
return math.floor( ( vx ^ 2 + vy ^ 2 + vz ^ 2 ) ^ 0.5 * unit + 0.5 )
 
end
 
function drawspeed()
dxDrawText(tostring( getspeed() ) ,911.0,654.0,982.0,685.0,tocolor(255,255,255,255),1.0,"bankgothic","right","left",false,false,false)
dxDrawText("Km/h", 989.0,664.0,1019.0,680.0,tocolor(255,255,255,255),1.0,"default","top","left",false,false,false)
end
addEventHandler("onClientRender", getRootElement(),drawspeed)

Link to comment

It was discussed that GTA units are meters and that a multiplier of 160.9 make no sense, at all.

The most accurate formula is, based on how much meters are travelled in one second:

For KM/H:

math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * 184

For MP/H:

math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * 114.33

(Using sqrt is more efficient than using exponents)

And about your question, you could do like that:

local unitsString = "km/h"
local speedMultiplier = 184
 
function getspeed()
return math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * speedMultiplier
end
 
function toggleUnits()
if unitsString == "km/h" then
       unitsString = "mph"
       speedMultiplier = 114.33
else
       unitsString = "km/h"
       speedMultiplier = 184
end
end
addCommandHandler( "ToggleSpeedoUnits", toggleUnits )
 
function drawspeed()
dxDrawText( tostring( math.floor( getspeed() ) ), 911.0, 654.0, 982.0, 685.0, tocolor(255,255,255,255), 1.0, "bankgothic", "right", "left", false, false, false)
dxDrawText( unitsString, 989.0, 664.0, 1019.0, 680.0, tocolor(255,255,255,255), 1.0, "default", "top", "left", false, false, false)
end
addEventHandler("onClientRender", getRootElement(), drawspeed)

For showing or hiding speedometer, same principle as toggleUnits but use add/RemoveEventHandler("onClientRender", .... ). You could make it more advanced and beautiful by making it fade in/out when you enter/leave vehicles.

Link to comment

Wow that's a bit advanced maths, I haven't learned it yet!

I'll learn a bit from you.

Anyway, what's VelX, VelY, VelZ? There's no variable yet...

It'll hide the digital speedo automatically if you enter / leave the vehicle. <-- Actually, this was because of errors, a bit lucky eh?

Anyway, I don't really need a fade in/out thingy cause it's for my Alternate_hud, I just need change colour according to the time.

Edited by Guest
Link to comment
It was discussed that GTA units are meters and that a multiplier of 160.9 make no sense, at all.

The 160.9 could be to compensate for the fact that GTA units don't really map exactly to meters... Possibly.

Link to comment

Don't know for sure, but I was not sure that the speed provided by GTA SA units is really exact too.

To me, it was a correct reading, now I realized it wasn't.

Anyway, the script still doesn't work, I added the getElementVelocity variable and changed command to bind, but it said that:

Error: :6: Attempt to arithmetic on upvalue "velX" (a boolean value)

What should I do?

Script:

local unitsString =  "Km/h"
local speedMultiplier = 184
local velX, velY, velZ = getElementVelocity( getPedOccupiedVehicle( getLocalPlayer() ) ) 
 
function getspeed()
return math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * speedMultiplier
end
 
function toggleUnits()
if unitsString == "Km/h" then
       unitsString = "mph"
       speedMultiplier = 114.33
else
       unitsString = "Km/h"
       speedMultiplier = 184
end
end
bindKey( "/", toggleUnits )
 
function drawspeed()
dxDrawText( tostring( math.floor( getspeed() ) ), 911.0, 654.0, 982.0, 685.0, tocolor(255,255,255,255), 1.0, "bankgothic", "right", "left", false, false, false)
dxDrawText( unitsString, 989.0, 664.0, 1019.0, 680.0, tocolor(255,255,255,255), 1.0, "default", "top", "left", false, false, false)
end
addEventHandler("onClientRender", getRootElement(), drawspeed)

Link to comment
Uhh ok. (Don't really understand that good)

How should I fix that? Cause I don't know what are the "checks" anyway. :(

You know, I'm a scripting newbie. That's why I ask. :roll:

maybe checking if the player its in a vehicle? i understand that from the "checks".

Link to comment

You use getElementVelocity only once and that is at the start of the resource so how do you expect it to work? You should use on every frame (onClientRender).

"Checks" are if statements to check if something is true or not.

local unitStr =  "Km/h"
local speedMultiplier = 160
local g_Me = getLocalPlayer();
 
function getspeed()
local velX, velY, velZ = getElementVelocity( getPedOccupiedVehicle( g_Me ) ) 
return math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * speedMultiplier
end
 
function toggleUnits(  )
   speedMultiplier = ( speedMultiplier < 160 ) and 160 or 100;
   unitStr = ( unitStr == "Km/h" ) and "mph" or "Km/h";
end
bindKey( "/", toggleUnits )
 
function drawspeed()
if isPedInVehicle( g_Me ) then -- this is the check you need, don't draw the speed if player is not in vehicle
dxDrawText( tostring( math.floor( getspeed() ) ), 911.0, 654.0, 982.0, 685.0, tocolor(255,255,255,255), 1.0, "bankgothic", "right", "left", false, false, false)
dxDrawText( unitStr, 989.0, 664.0, 1019.0, 680.0, tocolor(255,255,255,255), 1.0, "default", "top", "left", false, false, false)
end
end
addEventHandler("onClientRender", getRootElement(), drawspeed)

Edited by Guest
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...