Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I change the acceleration of this bodyposition/velocity (Vehicle)?

Asked by 6 years ago

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

--local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart
local car = script:WaitForChild("Car").Value
local stats = car:WaitForChild("Configurations")
local Raycast = require(car.CarScript.RaycastModule)
local object = car.Chassis -- your object here
local gear = stats.Gear
--local cameraType = Enum.CameraType.Follow

local movement = Vector2.new()
local gamepadDeadzone = 0.14

car.DriveSeat.Changed:connect(function(property)
    if property == "Steer" then
        movement = Vector2.new(car.DriveSeat.Steer, movement.Y)
    elseif property == "Throttle" then
        movement = Vector2.new(movement.X, car.DriveSeat.Throttle)
    end
end)

-- Input begin
--game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
--  if not gameProcessedEvent then
--      if inputObject.KeyCode == Enum.KeyCode.W then
--          movement = Vector2.new(movement.X, 1)
--      elseif inputObject.KeyCode == Enum.KeyCode.A then
--          movement = Vector2.new(-1, movement.Y)
--      elseif inputObject.KeyCode == Enum.KeyCode.S then
--          movement = Vector2.new(movement.X, -1)
--      elseif inputObject.KeyCode == Enum.KeyCode.D then
--          movement = Vector2.new(1, movement.Y)
--      end
--  end
--end)
--
--game:GetService("UserInputService").InputChanged:connect(function(inputObject, gameProcessedEvent)
--  --if not gameProcessedEvent then
--      if inputObject.KeyCode == Enum.KeyCode.Thumbstick1 then
--          --Gamepad support because yay
--          if inputObject.Position.magnitude >= gamepadDeadzone then
--              movement = Vector2.new(movement.X, inputObject.Position.Y)
--          else
--              movement = Vector2.new(movement.X, 0)
--          end
--      elseif inputObject.KeyCode == Enum.KeyCode.Thumbstick2 then
--          if inputObject.Position.magnitude >= gamepadDeadzone then
--              movement = Vector2.new(inputObject.Position.X, movement.Y)
--          else
--              movement = Vector2.new(0, movement.Y)
--          end
--      end
--  --end
--end)
--
---- Input end
--game:GetService("UserInputService").InputEnded:connect(function(inputObject, gameProcessedEvent)
--  if inputObject.KeyCode == Enum.KeyCode.W then
--      if movement.Y == 1 then
--          movement = Vector2.new(movement.X, 0)
--      end
--  elseif inputObject.KeyCode == Enum.KeyCode.A then
--      if movement.X == -1 then
--          movement = Vector2.new(0, movement.Y)
--      end
--  elseif inputObject.KeyCode == Enum.KeyCode.S then
--      if movement.Y == -1 then
--          movement = Vector2.new(movement.X, 0)
--      end
--  elseif inputObject.KeyCode == Enum.KeyCode.D then
--      if movement.X == 1 then
--          movement = Vector2.new(0, movement.Y)
--      end
--  end
--end)

local force = 0
local damping = 0

local mass = 0

for i, v in pairs(car:GetChildren()) do
    if v:IsA("BasePart") then
        mass = mass + (v:GetMass() * 196.2)
    end
end

force = mass * stats.Suspension.Value
damping = force / stats.Bounce.Value

local bodyVelocity = Instance.new("BodyVelocity", car.Chassis)
bodyVelocity.velocity = Vector3.new(0, 0, 0)
bodyVelocity.maxForce = Vector3.new(0, 0, 0)

local bodyAngularVelocity = Instance.new("BodyAngularVelocity", car.Chassis)
bodyAngularVelocity.angularvelocity = Vector3.new(0, 0, 0)
bodyAngularVelocity.maxTorque = Vector3.new(0, 0, 0)

local rotation = 0

local function UpdateThruster(thruster)
    --Make sure we have a bodythrust to move the wheel
    local bodyThrust = thruster:FindFirstChild("BodyThrust")
    if not bodyThrust then
        bodyThrust = Instance.new("BodyThrust", thruster)
    end
    --Do some raycasting to get the height of the wheel
    local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
    local thrusterHeight = (position - thruster.Position).magnitude
    if hit and hit.CanCollide then
        --If we're on the ground, apply some forces to push the wheel up
        bodyThrust.force = Vector3.new(0, ((stats.Height.Value - thrusterHeight)^2) * (force / stats.Height.Value^2), 0)
        local thrusterDamping = thruster.CFrame:toObjectSpace(CFrame.new(thruster.Velocity + thruster.Position)).p * damping
        bodyThrust.force = bodyThrust.force - Vector3.new(0, thrusterDamping.Y, 0)
    else
        bodyThrust.force = Vector3.new(0, 0, 0)
    end
end

--Shifting Function
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.E then
        if character.Humanoid.SeatPart == car.DriveSeat then
        if gear.Value < 18 then
            gear.Value = gear.Value + 1
            print(gear.Value)
        end
        end
    elseif inputObject.KeyCode == Enum.KeyCode.Q then
        if character.Humanoid.SeatPart == car.DriveSeat then
        if gear.Value > 0 and gear.Value <= 18 then
            gear.Value = gear.Value - 1
            print(gear.Value)
        end
        end
        end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

--A simple function to check if the car is grounded
local function IsGrounded()
    local hit, position = Raycast.new((car.Chassis.CFrame * CFrame.new(0, 0, (car.Chassis.Size.Z / 2) - 1)).p, car.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * (stats.Height.Value + 0.2))
    if hit and hit.CanCollide then
        return(true)
    end
    return(false)
end

--local oldCameraType = camera.CameraType
--camera.CameraType = cameraType

--spawn(function()
    while game:GetService("RunService").Heartbeat:wait() and car:FindFirstChild("DriveSeat") and character.Humanoid.SeatPart == car.DriveSeat do
        --game:GetService("RunService").RenderStepped:wait()
        if IsGrounded() then
            if movement.Y ~= 0 then
                local velocity = humanoidRootPart.CFrame.lookVector * movement.Y * stats.Speed.Value
                humanoidRootPart.Velocity = humanoidRootPart.Velocity:Lerp(velocity, 0.1)
                bodyVelocity.maxForce = Vector3.new(0, 0, 0)
            else
                bodyVelocity.maxForce = Vector3.new(mass / 2, mass / 4, mass / 2)
            end
            local rotVelocity = humanoidRootPart.CFrame:vectorToWorldSpace(Vector3.new(movement.Y * stats.Speed.Value / 50, 0, -humanoidRootPart.RotVelocity.Y * 5 * movement.Y))
            local speed = -humanoidRootPart.CFrame:vectorToObjectSpace(humanoidRootPart.Velocity).unit.Z
            rotation = rotation + math.rad((-stats.Speed.Value / 500) * movement.Y)
            if math.abs(speed) > 0.1 then
                rotVelocity = rotVelocity + humanoidRootPart.CFrame:vectorToWorldSpace((Vector3.new(0, -movement.X * speed * stats.TurnSpeed.Value, 0)))
                bodyAngularVelocity.maxTorque = Vector3.new(0, 0, 0)
            else
                bodyAngularVelocity.maxTorque = Vector3.new(mass / 4, mass / 2, mass / 4)
            end
            humanoidRootPart.RotVelocity = humanoidRootPart.RotVelocity:Lerp(rotVelocity, .1)

            --bodyVelocity.maxForce = Vector3.new(mass / 3, mass / 6, mass / 3)
            --bodyAngularVelocity.maxTorque = Vector3.new(mass / 6, mass / 3, mass / 6)
        else
            bodyVelocity.maxForce = Vector3.new(0, 0, 0)
            bodyAngularVelocity.maxTorque = Vector3.new(0, 0, 0)
        end

        for i, part in pairs(car:GetChildren()) do
            if part.Name == "Thruster" then
                UpdateThruster(part)
            end
        end
    end
    for i, v in pairs(car:GetChildren()) do
        if v:FindFirstChild("BodyThrust") then
            v.BodyThrust:Destroy()
        end
    end
    bodyVelocity:Destroy()
    bodyAngularVelocity:Destroy()
    gear.Value = 0
    --camera.CameraType = oldCameraType
    script:Destroy()
--end)
0
How did you have the patience to write almost 200 lines of code what could be written in 50-75? User#19524 175 — 6y
0
This script came from a jeep inside one the pre-built maps you can choose from when starting a new game. Everything related to BodyGyros, BodyPosition, etc is new to me, so I was using this script as a bases to learn from, and to understand how it works. But i'm stumped on acceleration. WizyTheNinja 834 — 6y

Answer this question