Help improving my cape script

I have this cape script, but I think the code looks messy. Can I somehow improve it?

local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")
local torso = char:WaitForChild("Torso")
local cape = workspace.Cape:Clone()

local weld = Instance.new('Motor6D')
weld.Part0 = torso
weld.Part1 = cape
weld.Parent = cape
cape.Parent = char

local c0_add = CFrame.new(0,torso.Size.Y/2,torso.Size.Z/2)
weld.C0 = c0_add
weld.C1 = CFrame.new(0,cape.Size.Y/2,0) * CFrame.Angles(0,math.pi,0)

local rcf0 = root.CFrame
runService.RenderStepped:Connect(function(dt)
	local rcf1 = root.CFrame
	
	local moved = human.MoveDirection
	local moveds = moved * human.WalkSpeed
	local camcf = camera.CFrame
	local vel = (rcf1.Position-rcf0.Position)
	local roty = root.Orientation.Y
	local uvelcf = CFrame.new(Vector3.new(),vel)
	local rvel = vel.Magnitude > 0 and (CFrame.Angles(0,math.rad(-roty),0)*uvelcf).LookVector or Vector3.new()
	rvel *= Vector3.new(1,0,1)*human.WalkSpeed
	
	local xside =
		(userInputService.MouseBehavior == Enum.MouseBehavior.LockCenter and moved:Dot(camcf.LookVector))
		or
		(moved.Magnitude > 0 and 1 or 0)
	
	local horisontal = moved.Magnitude > 0 and (rcf1.Position*Vector3.new(1,0,1)-rcf0.Position*Vector3.new(1,0,1)).Magnitude * human.WalkSpeed or 0
	local vertical = -root.Velocity.Y
	local xangle = math.clamp((horisontal*10+vertical*3)*xside+vertical*3-rvel.Z*3,-5,150)
	
	local p0 = rcf0.LookVector
	local p1 = rcf1.LookVector
	local _,y0,_ = rcf0:ToOrientation()
	local _,y1,_ = rcf1:ToOrientation()
	local s = math.clamp(y0-y1,-1,1)
	local dot = math.atan2(p1:Cross(p0).Magnitude,p1:Dot(p0))/math.pi
	local side = math.clamp(dot*180*s*6,-100,100)
	local c0 = c0_add*CFrame.Angles(-math.rad(xangle),math.rad(side+rvel.Y*3),math.rad(side*3-rvel.X*3))
	
	game.TweenService:Create(weld,TweenInfo.new(.2,Enum.EasingStyle.Linear),{C0=c0}):Play()
	
	rcf0 = rcf1
end)

bruh_1721

3 Likes

Rather than doing the cape animations using a script why not use some sort of constraint?

Roblox physics is very unreliable

1 Like

It’s not as bad as people think! As long as you don’t make something REALLY messy with lot’s of cancollides on, it should be mostly fine! But I see your point… Hmm. So right now you’re changing it based on Velocity? And you want it to look less messy. Well… I guess just use functions instead of calculating it each time! Makes it look a lot neater, as well as avoid having to many variables as it can make it look messy as well. Definetly just make a function for the Render Stepped though, looks neater!