Jump to content

cruise control?


jkub

Recommended Posts

i was recently looking at someone elses post and I saw someone say somthin about cruise control. I thought that was a nice start for a noob such as myself so i jocked his style and came up with something. I have not got to test it yet but I know its not gonna work lol

  
     
function cruise ( thePlayer, theVehicle ) 
  
local theVehicle = getPlayerOccupiedVehicle ( thePlayer ) 
local speedx, speedy, speedz = getElementVelocity ( theVehicle ) 
local speed = getElementVelocity 
  
    setElementVelocity ( theVehicle, speedx, speedy, speedz ) 
    if speed < speedx and speedy and speedz then 
        setElementVelocity ( theVehicle, speedx, speedy, speedz ) 
    end 
end 
  
addCommandHandler ( "cruise", cruise ) 
  
bindKey ( "thePlayer", "c", "down" ) 
  
  
  
  

Link to comment

There are two ways in which you could make a cruise control: an easy one, and one that would involve a bit of math. In both cases I would recommend making it a clientside script.

The easy way would be to use setControlState. From time to time check the current speed - if it's too low, start pressing the forward key, if it's too high, release the key. This will make the car alternate between accelerating and rolling.

The other way would be to get the velocity vector (speedx/y/z), re-scale it to the speed you want, and apply the result to the car with setElementVelocity. If you're not sure how to do this, I can elaborate.

Also, this

local speed = getElementVelocity 

doesn't work. What you are doing there is storing the getElementVelocity function itself in the "speed" variable. In order to get the actual speed of the car, you need to use the Pythagorean theorem on speedx/y/z. You don't even have to type out the formula yourself, MTA provides it (getDistanceBetweenPoints3D).

So, to get the speed of the car, you would do this:

local speedx, speedy, speedz = getElementVelocity ( theVehicle ) 
local speed = getDistanceBetweenPoints3D(0, 0, 0, speedx, speedy, speedz) 

Or shorter, if you don't need speedx/y/z:

local speed = getDistanceBetweenPoints3D(0, 0, 0, getElementVelocity ( theVehicle )) 

Good luck.

Link to comment

ive tryed using the setControlState method and I used settimer to mess with the acceleration control

setControlState ( thePlayer, "accelerate", true ) -- I assume that would lock the accelerator 
timer = setTimer ( setControlState, 200, 0, thePlayer, "accelerate", false ) 

--this would disable the accelerator every 0.2 seconds

but how would I constantly rotate between enabling and disabling acceleration because I know that above would enable the accel then 0.2 seconds later disable it. how would i loop

Link to comment

Something like this would do. (Mind you, this code is a client side script, note the missing "player" argument in the call to setControlState.)

local accelerate = true        -- Whether or not to press the accelerate button on next timer trigger 
  
function toggleAccelerate() 
    setControlState("accelerate", accelerate)      -- Pass the current value of the accelerate variable to setControlState 
    accelerate = not accelerate           -- Invert the value of the accelerate variable (true -> false, false -> true) 
end 
  
setTimer(toggleAccelerate, 200, 0) 

Of course this approach is rather simplistic as it doesn't take into account the actual speed of the car. For example, when going uphill, you will need to hold the accelerate button longer in order to maintain your speed. So instead of just toggling the control state on each timer tick, you would get the current speed of the car and press/release the accelerate button accordingly.

Link to comment

Psuedo code for what I did:

On key press: get speed of vehicle, store this into a variable. Start a timer with pretty low interval (like 100)

On timer tick: get speed of vehicle. If > saved speed, turn off accel. control. Else, turn on.

On accel, brake, or second key press: stop timer and clear the variable.

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