How would I change the acceleration of this bodyposition/velocity (Vehicle)?
Hello all! Back again with another question.
I have created a vehicle using bodygyro, bodyposition,etc. I used the scripts from the jeep model on the race prebuilt map and modified them slightly to meet my needs.
Basically I am creating a gearing system for my vehicles. Depending on what gear you are in, depends on how fast you are able to go.
So far I have everything working, the vehicle drives, turns, and even can shift gears. However, my issue is that the vehicle accelerates very quickly, almost max speed instantly. My shifting script is based on speed, if you aren't going fast enough to shift gears, it won't let you, but being as the vehicle accelerates instantly, players can just spam the shift gear button and would defeat the purpose of shifting.
I'm basically trying to figure out how to make the velocity of the vehicle take time to reach its max. I know about MaxThrust, MaxTorque, and MaxForce, however with the Jeep script, editing any value also affects the turning. I was able to make the script slowly accelerate by editing the Lerp value, however that also made the turning wonky, causing it to go really fast while turning, or not turn at all.
My question is what values do I need to be adjusting, need to add into the current script, to make it not accelerate to max speed instantly?
I know about D and P aswell, but changing those values didn't affect anything. My assumption is because those values are used in relation to destination. Obviously a vehicle driving around has no set destination, therefor it can't damper it.
LocalCarScript
002 | local player = game.Players.LocalPlayer |
003 | local character = player.Character |
004 | local humanoidRootPart = character.HumanoidRootPart |
005 | local car = script:WaitForChild( "Car" ).Value |
006 | local stats = car:WaitForChild( "Configurations" ) |
007 | local Raycast = require(car.CarScript.RaycastModule) |
008 | local object = car.Chassis |
009 | local gear = stats.Gear |
012 | local movement = Vector 2. new() |
013 | local gamepadDeadzone = 0.14 |
015 | car.DriveSeat.Changed:connect( function (property) |
016 | if property = = "Steer" then |
017 | movement = Vector 2. new(car.DriveSeat.Steer, movement.Y) |
018 | elseif property = = "Throttle" then |
019 | movement = Vector 2. new(movement.X, car.DriveSeat.Throttle) |
083 | for i, v in pairs (car:GetChildren()) do |
084 | if v:IsA( "BasePart" ) then |
085 | mass = mass + (v:GetMass() * 196.2 ) |
089 | force = mass * stats.Suspension.Value |
090 | damping = force / stats.Bounce.Value |
092 | local bodyVelocity = Instance.new( "BodyVelocity" , car.Chassis) |
093 | bodyVelocity.velocity = Vector 3. new( 0 , 0 , 0 ) |
094 | bodyVelocity.maxForce = Vector 3. new( 0 , 0 , 0 ) |
096 | local bodyAngularVelocity = Instance.new( "BodyAngularVelocity" , car.Chassis) |
097 | bodyAngularVelocity.angularvelocity = Vector 3. new( 0 , 0 , 0 ) |
098 | bodyAngularVelocity.maxTorque = Vector 3. new( 0 , 0 , 0 ) |
102 | local function UpdateThruster(thruster) |
104 | local bodyThrust = thruster:FindFirstChild( "BodyThrust" ) |
105 | if not bodyThrust then |
106 | bodyThrust = Instance.new( "BodyThrust" , thruster) |
109 | local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector 3. new( 0 , - 1 , 0 )) * stats.Height.Value) |
110 | local thrusterHeight = (position - thruster.Position).magnitude |
111 | if hit and hit.CanCollide then |
113 | bodyThrust.force = Vector 3. new( 0 , ((stats.Height.Value - thrusterHeight)^ 2 ) * (force / stats.Height.Value^ 2 ), 0 ) |
114 | local thrusterDamping = thruster.CFrame:toObjectSpace(CFrame.new(thruster.Velocity + thruster.Position)).p * damping |
115 | bodyThrust.force = bodyThrust.force - Vector 3. new( 0 , thrusterDamping.Y, 0 ) |
117 | bodyThrust.force = Vector 3. new( 0 , 0 , 0 ) |
122 | function onKeyPress(inputObject, gameProcessedEvent) |
123 | if inputObject.KeyCode = = Enum.KeyCode.E then |
124 | if character.Humanoid.SeatPart = = car.DriveSeat then |
125 | if gear.Value < 18 then |
126 | gear.Value = gear.Value + 1 |
130 | elseif inputObject.KeyCode = = Enum.KeyCode.Q then |
131 | if character.Humanoid.SeatPart = = car.DriveSeat then |
132 | if gear.Value > 0 and gear.Value < = 18 then |
133 | gear.Value = gear.Value - 1 |
140 | game:GetService( "UserInputService" ).InputBegan:connect(onKeyPress) |
143 | local function IsGrounded() |
144 | local hit, position = Raycast.new((car.Chassis.CFrame * CFrame.new( 0 , 0 , (car.Chassis.Size.Z / 2 ) - 1 )).p, car.Chassis.CFrame:vectorToWorldSpace(Vector 3. new( 0 , - 1 , 0 )) * (stats.Height.Value + 0.2 )) |
145 | if hit and hit.CanCollide then |
155 | while game:GetService( "RunService" ).Heartbeat:wait() and car:FindFirstChild( "DriveSeat" ) and character.Humanoid.SeatPart = = car.DriveSeat do |
158 | if movement.Y ~ = 0 then |
159 | local velocity = humanoidRootPart.CFrame.lookVector * movement.Y * stats.Speed.Value |
160 | humanoidRootPart.Velocity = humanoidRootPart.Velocity:Lerp(velocity, 0.1 ) |
161 | bodyVelocity.maxForce = Vector 3. new( 0 , 0 , 0 ) |
163 | bodyVelocity.maxForce = Vector 3. new(mass / 2 , mass / 4 , mass / 2 ) |
165 | local rotVelocity = humanoidRootPart.CFrame:vectorToWorldSpace(Vector 3. new(movement.Y * stats.Speed.Value / 50 , 0 , -humanoidRootPart.RotVelocity.Y * 5 * movement.Y)) |
166 | local speed = -humanoidRootPart.CFrame:vectorToObjectSpace(humanoidRootPart.Velocity).unit.Z |
167 | rotation = rotation + math.rad((-stats.Speed.Value / 500 ) * movement.Y) |
168 | if math.abs(speed) > 0.1 then |
169 | rotVelocity = rotVelocity + humanoidRootPart.CFrame:vectorToWorldSpace((Vector 3. new( 0 , -movement.X * speed * stats.TurnSpeed.Value, 0 ))) |
170 | bodyAngularVelocity.maxTorque = Vector 3. new( 0 , 0 , 0 ) |
172 | bodyAngularVelocity.maxTorque = Vector 3. new(mass / 4 , mass / 2 , mass / 4 ) |
174 | humanoidRootPart.RotVelocity = humanoidRootPart.RotVelocity:Lerp(rotVelocity, . 1 ) |
179 | bodyVelocity.maxForce = Vector 3. new( 0 , 0 , 0 ) |
180 | bodyAngularVelocity.maxTorque = Vector 3. new( 0 , 0 , 0 ) |
183 | for i, part in pairs (car:GetChildren()) do |
184 | if part.Name = = "Thruster" then |
189 | for i, v in pairs (car:GetChildren()) do |
190 | if v:FindFirstChild( "BodyThrust" ) then |
191 | v.BodyThrust:Destroy() |
194 | bodyVelocity:Destroy() |
195 | bodyAngularVelocity:Destroy() |