Hello, I am creating a custom car and I would like to control it like a real car with my keyboard (W to go foward, S to go back, A to go on the left and D to go on the right).
There are properties in a VehicleSeat like ThrottleFloat and SteerFloat that store the input, so you just have to use those properties in a script that makes the car move depending on those properties
Incase you still feel like re-inventing the wheel, this is a VERY basic example of moving models with UIS
local UserInputService = game:GetService("UserInputService")
local Car = workspace:WaitForChild("Car")
local Moving = false
UserInputService.InputBegan:Connect(function(input, _gameProceesedEvent)
if input.KeyCode == Enum.KeyCode.W then
if Moving then return end --> Don't Do Anything if Already Moving
print("Started!")
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() + Vector3.new(0,0,-1))
print("Moving!")
task.wait(0.01)
end
elseif input.KeyCode == Enum.KeyCode.S then
if Moving then return end --> Don't Do Anything if Already Moving
print("Started!")
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() + Vector3.new(0,0,1))
print("Moving!")
task.wait(0.01)
end
end
end)
UserInputService.InputEnded:Connect(function(input, _gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.W then
if not Moving then return end
Moving = false
elseif input.KeyCode == Enum.KeyCode.S then
if not Moving then return end
Moving = false
end
end)
It works with CFrame and not Vector just how I move it server side…
local UserInputService = game:GetService("UserInputService")
local Car = script.Parent.Parent
local seat = Car:FindFirstChild("MainSeat")
local rotationSpeed = 1
local Moving = false
UserInputService.InputBegan:Connect(function(input, _gameProceesedEvent)
if input.KeyCode == Enum.KeyCode.W and not _gameProceesedEvent and seat.Occupant then
if Moving then return end
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() * CFrame.new(0,0,1))
task.wait(0.01)
end
elseif input.KeyCode == Enum.KeyCode.S and not _gameProceesedEvent and seat.Occupant then
if Moving then return end
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() * CFrame.new(0,0,-1))
task.wait(0.01)
end
elseif input.KeyCode == Enum.KeyCode.D and not _gameProceesedEvent and seat.Occupant then
if Moving then return end
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() * CFrame.Angles(0,-math.rad(1),0))
task.wait(0.01)
end
elseif input.KeyCode == Enum.KeyCode.A and not _gameProceesedEvent and seat.Occupant then
if Moving then return end
Moving = true
while Moving == true do
Car:PivotTo(Car:GetPivot() * CFrame.Angles(0,math.rad(1),0))
task.wait(0.01)
end
end
end)
UserInputService.InputEnded:Connect(function(input, _gameProceesedEvent)
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
if not Moving then return end
Moving = false
end
end)