Hi, I’m editing a horse by @Omnisxiii2 . My goal is for players to be able to rotate their torso on where they’re looking while on the horse.
I’ve been working off of a script created by @MrNicNac which allows a character’s torso to rotate depending on where the camera is looking, and the script itself works great, however I’m trying to make it so the rotation only occurs when a player is on horseback. I’ve gotten the two pieces of code to work fine until the player leaves their seat, and their torso is stuck in the position they were facing while they were seated.
I’ve tried making the torso go back to its original orientation, but I couldn’t figure it out. Is there any way to fix this?
Thanks for any help in advance!!
This code is within a localscript in the seat to ride the horse.
EDIT: The code isn’t inside the seat to ride the horse, it’s in a localscript within a script which is inside the horse model.
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char.Humanoid
local horse = humanoid.SeatPart.Parent
local adjustevent = horse:WaitForChild("HorseMovementAdjustEvent")
local rideranimroot = horse.Animations.Rider
rideranim = humanoid:LoadAnimation(rideranimroot)
local allowfirearms = horse.AllowFirearms.Value
local cam = workspace.CurrentCamera
local rootPart = char.PrimaryPart
local upperTorso = char:WaitForChild("UpperTorso")
local waistJoint = upperTorso:WaitForChild("Waist")
-- Services
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
-- Globals
local currentSpeed = 0;
local currentTween = nil;
wait (0.1)
rideranim:Play()
humanoid:UnequipTools()
userInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
if not gameProcessedEvent then
--print "Input for horse"
if inputObject.KeyCode == Enum.KeyCode.W then
adjustevent:FireServer("forward")
elseif inputObject.KeyCode == Enum.KeyCode.S then
adjustevent:FireServer("slow")
elseif inputObject.KeyCode == Enum.KeyCode.D then
adjustevent:FireServer("right")
elseif inputObject.KeyCode == Enum.KeyCode.A then
adjustevent:FireServer("left")
elseif inputObject.KeyCode == Enum.KeyCode.E then
adjustevent:FireServer("jump")
end
end
end)
userInputService.InputEnded:connect(function(inputObject, gameProcessedEvent)
if not gameProcessedEvent then
if inputObject.KeyCode == Enum.KeyCode.W then
adjustevent:FireServer("stopforward")
elseif inputObject.KeyCode == Enum.KeyCode.S then
adjustevent:FireServer("stopslow")
elseif inputObject.KeyCode == Enum.KeyCode.D then
adjustevent:FireServer("stopright")
elseif inputObject.KeyCode == Enum.KeyCode.A then
adjustevent:FireServer("stopleft")
end
end
end)
humanoid.Changed:connect(function()
if humanoid.SeatPart == nil then
rideranim:Stop()
humanoid:UnequipTools()
end
end)
char.DescendantAdded:Connect(function(NewChild)
if NewChild:IsA("Tool") and allowfirearms == false then
--Tool equipped check for weapon
--local derp = NewChild:FindFirstChild("NoHorse")
local derp = NewChild:FindFirstChild("Ammo")
if derp then
wait (0.5)
humanoid:UnequipTools()
end
end
end)
humanoid.Running:Connect(function(speed)
currentSpeed = speed
end)
-- This was actually not needed to be disabled
--human.AutoRotate = false
local originalWaistC1 = waistJoint.C1
local maxRotation = math.rad(65)
runService:BindToRenderStep("vanityCamera", 400, function()
local targetPosition = (cam.CFrame * CFrame.new(0,0,-1000)).p
local torsoFront = rootPart.CFrame.LookVector
local torsoRight = rootPart.CFrame.RightVector
local vectorToTarget = (targetPosition - rootPart.Position)
local rotation = math.atan2(torsoRight:Dot(vectorToTarget), torsoFront:Dot(vectorToTarget))
-- Clamp the rotation
if (rotation < -maxRotation) then
rotation = -maxRotation
elseif (rotation > maxRotation) then
rotation = maxRotation
end
if (math.abs(rotation) == maxRotation and currentSpeed <= 0.5) then
-- Rotate the bottom half to face the new direction
local newRootPartCFrame = CFrame.new(rootPart.Position, Vector3.new(
targetPosition.X, rootPart.Position.Y, targetPosition.Z
))
currentTween = tweenService:Create(rootPart, TweenInfo.new(0.33), {
CFrame = newRootPartCFrame
})
currentTween:Play()
else
if (currentTween and currentSpeed > 0.5) then
if (currentTween.PlaybackState == Enum.PlaybackState.Playing) then
currentTween:Cancel()
end
end
waistJoint.C1 = CFrame.Angles(0,rotation,0) * originalWaistC1
end
end)
Example of what’s happening in game:
Before sitting on the horse, my torso is able to rotate upwards and downwards but not side-to-side, as intended
Riding the horse, I can look and rotate side-to-side, as well as upwards and downwards, as intended
Dismounted from the horse, my torso is rotated to where I was last looking towards on horseback, and is stuck in this position, unintended.