Character Lean Back and Forth When Walking

I have a script here that tilts the character sightly when walk left to right, however, I want it to do the same thing when going back and forth. This is my first time seeing this type of script so I have no clue how to do this any help is greatly appreciated

local TweenService = game:GetService("TweenService")
root = script.Parent:WaitForChild("HumanoidRootPart")
local dir, vel
local angle = 0
local original = root.RootJoint.C0
--looping code
hb = game:GetService("RunService")

hb.Heartbeat:Connect(function()
	vel = root.Velocity * Vector3.new(1,0,1)
	if vel.Magnitude > 2  then
		dir = vel.Unit
		angle = root.CFrame.RightVector:Dot(dir)/7--will be between -1 and 1 based on how far off it is

	else

		angle = 0


	end

	local tweenInfo = TweenInfo.new(
		.2, -- Time
		Enum.EasingStyle.Quad, -- EasingStyle
		Enum.EasingDirection.Out, -- EasingDirection
		0, -- RepeatCount (when less than zero the tween will loop indefinitely)
		false, -- Reverses (tween will reverse once reaching it's goal)
		0 -- DelayTime
	)

	local tween = TweenService:Create(root.RootJoint, tweenInfo, {C0 = original*CFrame.Angles(0, -angle, 0)})

	tween:Play()
	--root.RootJoint.C0 = original*CFrame.Angles(0, -angle, 0)
end)```

Question, did you make this script?

If it’s no, you obviously won’t know what to do with this.

I will tell you how to do it, but not show.
First off, understand what you’re dealing with. It’s changing a Motor6D or a Motor’s C0, which obviously tells you how it tilts the character left and right when walking. If you want to do the same thing but when back and forth, you would have to understand more about CFrames, and a bit more knowledge about things like these.


I may seem a bit rude here, but I heavily do not like posts like these, when you look at another person’s code but want to do the same thing but back and forth, it makes me think about plagiarism, where you’re just gonna edit the script and claim you made it.

Well Im not stealing this script it was a free model and I know an ok amount about CFrames just not this I never learned about some of the things in this script thats why I was asking for help.

2 Likes

why you gotta be so rude about it man. He is just asking for some guidance, if the og code is open sourced i dont see a problem.

if you are still looking for a solution, here is the script:

local RunService = game:GetService("RunService")

local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
local dir, vel
local angleS = 0
local angleF = 0
local original = rootPart:WaitForChild("RootJoint").C0

RunService.Heartbeat:Connect(function()
	vel = rootPart.Velocity
	if vel.Magnitude > 2  then
		dir = vel.Unit
		angleS = rootPart.CFrame.RightVector:Dot(dir)/7
		angleF = rootPart.CFrame.LookVector:Dot(dir)/7
	else
		angleS = 0
		angleF = 0
	end

	local tweenInfo = TweenInfo.new(
		.2,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out
	)

	local tween = TweenService:Create(rootPart.RootJoint, tweenInfo, {C0 = original*CFrame.Angles(angleF, -angleS, 0)})

	tween:Play()
end)

So basically, we just make a new variable for the front lean and call it angleF and we calculate it the same way as angleS but instead of using the RightVector we use LookVector put it in a local script under starter character scripts.