Jump to content

I ask for help


Recommended Posts

Hello everyone, friends, please help me, I'm very weak in MTA scripts ... 
Please understand, just help please =). I would like to implement a speed limit for a broken vehicle...

 

here is the code, i want to add a vehicle gain limit here, not only the maximum speed as stated here, but also the maximum acceleration, up to 5 when the vehicle broke down

 

local MAX_VELOCITY = 0.1
local MAX_BROKEN_VELOCITY = 0.15
local LAST_LENGTH = 0

function ParseVelocity()
	local vehicle = localPlayer.vehicle
	if not vehicle then return end

	if vehicle.occupants[ 0 ] ~= localPlayer then return end

	local side_vector = Vector2( vehicle.velocity.x, vehicle.velocity.y )

	local fake_vector = vehicle.velocity

	local veh_vector = vehicle.matrix.forward
	veh_vector.z = 0

	local angle = ( fake_vector:getLength() * veh_vector:getLength() ) / fake_vector:dot( veh_vector )


	if getElementHealth( vehicle ) <= 360 then
		if side_vector:getLength( ) > MAX_BROKEN_VELOCITY then
			side_vector = side_vector:getNormalized() * MAX_BROKEN_VELOCITY
			vehicle.velocity = Vector3( side_vector.x, side_vector.y, vehicle.velocity.z )
			return
		end
	end


	if angle <= 0.9 and side_vector:getLength() > MAX_VELOCITY and vehicle.velocity.z > -0.1 then
		if LAST_LENGTH <= MAX_VELOCITY then
			LAST_LENGTH = MAX_VELOCITY
			side_vector = side_vector:getNormalized() * MAX_VELOCITY
			vehicle.velocity = Vector3( side_vector.x, side_vector.y, vehicle.velocity.z )
		end
	else
		LAST_LENGTH = side_vector:getLength()
	end
end

function onClientVehicleEnter_handler( player )
	if player ~= localPlayer then return end
	onClientVehicleExit_handler( player )
	VELOCITY_TIMER = Timer( ParseVelocity, 50, 0 )
	addEventHandler( "onClientVehicleExit", root, onClientVehicleExit_handler )
end
addEventHandler( "onClientVehicleEnter", root, onClientVehicleEnter_handler )

function onClientVehicleExit_handler( player )
	if player ~= localPlayer then return end
	if isTimer( VELOCITY_TIMER ) then killTimer( VELOCITY_TIMER ) end
	removeEventHandler( "onClientVehicleExit", root, onClientVehicleExit_handler )
end

if localPlayer.vehicle then onClientVehicleEnter_handler( localPlayer ) end

 

Link to comment
21 hours ago, Citizen said:

Did you consider using setVehicleHandling instead ?

For me, all you are doing can be done using that function. It will be easier and will prevent stressing the player's CPU as well.

 

I'm sorry, I downloaded this resource from the Internet and I don't understand it, I just need to reduce the acceleration of transport to a value of 5, in case of a breakdown ... don't help me, I beg you

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