Hello, I’m currently making a horse for my game. The riding system is unique, the horse is being rotated using a bodygyro and being moved forward by Humanoid:Move(). I want the horse to move forward when the player starts holding W. It partially works but it ignores the orientation for some reason (it goes in one direction only)
Video:
https://gyazo.com/eeadaee0ca77b11c21652940bee8dfda
Code:
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local horse = script:WaitForChild("Horse").Value
local speed = 0
local keypressed = false
function Dismount()
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
humanoid.Jump = true
humanoid.JumpPower = 50
speed = 0
end
local horseGui = script:WaitForChild("HorseGui")
horseGui.Parent = player.PlayerGui
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
humanoid.JumpPower = 0
local rideAnimation = humanoid:LoadAnimation(horse.Animations.Ride)
rideAnimation:Play()
local movement = Vector3.new(0, 0, 0)
userInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
if not gameProcessedEvent then
if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.Up then
keypressed = true
while keypressed do
if speed >= 5 then speed = 5 return end
wait(.5)
speed += 0.25
print(tostring(speed))
end
elseif inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.Down then
keypressed = true
while keypressed do
if speed <= 0 then speed = 0 return end
wait(.5)
speed -= 0.25
print(tostring(speed))
end
elseif inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.Left then
keypressed = true
while keypressed do
wait(.000001)
horse.HumanoidRootPart.BodyGyro.CFrame = horse.HumanoidRootPart.BodyGyro.CFrame * CFrame.Angles(0,0.1,0)
end
elseif inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Right then
keypressed = true
while keypressed do
wait(.0000001)
horse.HumanoidRootPart.BodyGyro.CFrame = horse.HumanoidRootPart.BodyGyro.CFrame * CFrame.Angles(0,-0.1,0)
end
elseif inputObject.KeyCode == Enum.KeyCode.Space then
local ray = Ray.new(horse.HumanoidRootPart.Position + Vector3.new(0, -4.3, 0), Vector3.new(0, -1, 0))
local hit, position = game.Workspace:FindPartOnRay(ray, horse)
if hit and hit.CanCollide then
horse.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
elseif inputObject.KeyCode == Enum.KeyCode.LeftControl then
Dismount()
end
end
end)
userInputService.InputEnded:connect(function(inputObject, gameProcessedEvent)
if not gameProcessedEvent then
if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.Up then
keypressed = false
elseif inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.Down then
keypressed = false
elseif inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.Left then
keypressed = false
elseif inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Right then
keypressed = false
end
end
end)
spawn(function()
while horse.Seat.Occupant == humanoid do
game:GetService("RunService").RenderStepped:wait()
horse.Humanoid:Move(Vector3.new(speed + horse.HumanoidRootPart.Orientation.X, 0, 0, false))
end
horseGui:Destroy()
Dismount()
horse.Humanoid:Move(Vector3.new())
rideAnimation:Stop()
script:Destroy()
end)