#Steering Wheel : Car moving Right Alaways, WHY?!

1 messages · Page 1 of 1 (latest)

golden hornet
#

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local remote = ReplicatedStorage:WaitForChild("PedalInputEvent")
local truck = workspace:WaitForChild("Truck")
local primaryPart = truck.PrimaryPart
local steeringWheel = truck:WaitForChild("SteeringWheel")

-- Movement settings
local maxSpeed = 50
local acceleration = 5
local deceleration = 0.3
local brakeForce = 10
local turnSpeed = 50 -- Degrees per second at full steering

local moveSpeed = 0
local moving = false
local braking = false

remote.OnServerEvent:Connect(function(player, action)
if action == "StartMoving" then
moving = true
elseif action == "StopMoving" then
moving = false
elseif action == "StartBraking" then
braking = true
elseif action == "StopBraking" then
braking = false
end
end)

RunService.Heartbeat:Connect(function(dt)
if not primaryPart then return end

-- Handle speed changes
if braking then
    moveSpeed = math.max(moveSpeed - brakeForce, 0)
elseif moving then
    moveSpeed = math.min(moveSpeed + acceleration, maxSpeed)
else
    moveSpeed = math.max(moveSpeed - deceleration, 0)
end

#

-- Steering
local relativeCF = primaryPart.CFrame:toObjectSpace(steeringWheel.CFrame)
local _, y, _ = relativeCF:ToEulerAnglesYXZ()
local steerFactor = -math.clamp(math.deg(y) / 45, -1, 1)

if moveSpeed > 0 then
    -- Get current orientation and position
    local currentCFrame = primaryPart.CFrame

    -- Rotate based on steer factor
    if math.abs(steerFactor) > 0.01 then
        local turnAngle = math.rad(steerFactor * turnSpeed * dt)
        currentCFrame = currentCFrame * CFrame.Angles(0, turnAngle, 0)
    end

    -- Move forward in the truck's local -Z (forward) direction
    local forward = currentCFrame.LookVector
    local offset = forward * moveSpeed * dt

    truck:SetPrimaryPartCFrame(currentCFrame + offset)
end

end)

wet narwhal
#

probably because steerfactor is wrong is my guess

#

idk, probably shouldn't rely on ai for things like this 🤷

golden hornet
#

yea, im just rookie to scripting, and i asked chatGPT for some help XD