i want the character to automatically control a vehicle when spawned without the character touching the vehicle
2 Likes
You can simply have a script controlling the vehicle by changing the VehicleSeat’s Properties (Throttle and Steer)
Setting the Throttle property to 1 would make your vehicle go fowards. And setting it to -1 would make your vehicle reverse. And setting it to 0 would just stop your vehicle
And for steering, setting the Steer property to 1, would turn your vehicle right. And setting it to -1 would turn your vehicle left. And setting it to 0 would stop your vehicle turning.
Heres an example:
VehicleSeat = Vehicle.VehicleSeat
local UIS = game:GetService("UserInputService") -- Allows the script to detect which key we are pressing, which are: U, H, J, K
function Throttle(Value) -- 1 = forward, 0 = stop, -1 = reverse
VehicleSeat.Throttle = Value
end
function Steer(Value) -- 1 = right, 0 = middle, -1 = left
VehicleSeat.Steer = Value
end
UIS.InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.U then -- Pressing U will make the car go forwards
Throttle(1)
end
if inputObject.KeyCode == Enum.KeyCode.J then -- Pressing J will make the car go backwards
Throttle(-1)
end
if inputObject.KeyCode == Enum.KeyCode.H then -- Pressing H will steer the left
Steer(-1)
end
if inputObject.KeyCode == Enum.KeyCode.K then -- Pressing K will steer the car right
Steer(1)
end
end)
UIS.InputEnded:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.U then -- Releasing U will make the car stop
Throttle(0)
end
if inputObject.KeyCode == Enum.KeyCode.J then -- Releasing J will make the car stop
Throttle(0)
end
if inputObject.KeyCode == Enum.KeyCode.H then -- Releasing H will make the car go straight
Steer(0)
end
if inputObject.KeyCode == Enum.KeyCode.K then -- Releasing K will make the car go straight
Steer(0)
end
end)
Hope this helps!
9 Likes