Character movement tilt

Recently I have been trying to make a movement script that makes the player tilt in the direction the player moves.

Isue:

  • The code seems to be delayed by something for some reason and I don’t know why
  • I also want to make it so the tilt will be based on speed. For example, when walking the tilt is very slight. But when running at a high speed the tilt is very harsh. I don’t know how to do this.

My current code:

task.wait(1.5)
local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(script.Parent)
local TweenService = game:GetService("TweenService")
local root = Player.Character.HumanoidRootPart
local dir, vel
local angle = 0
local angle2 = 0
local original = root.RootJoint.C0
--looping code
local hb = game:GetService("RunService")

hb.Heartbeat:Connect(function()
	if Player.Character.Humanoid.Health >= 1 then
		vel = root.Velocity * Vector3.new(1,0,1)

		if vel.Magnitude > 2  then
			dir = vel.Unit
			angle = root.CFrame.RightVector:Dot(dir)/6.5
			angle2 = root.CFrame.LookVector:Dot(dir)/6.5
		else
			angle = 0
			angle2 = 0
		end

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

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

		tween:Play()
	end
end)

Current Issue:
robloxapp-20230405-2033382.wmv (1.2 MB)

HELP!!!

Use Lerp instead of tween service, it could work better I think.

Nah that makes it just teleports to the position and doesn’t do it smoothly plus it’s still delayed.

TweenService uses Interpolation, which is known as Lerp.

So doing this is the same thing, and would give you the exact same result.

Which it did so nice job on the big brains :+1:

1 Like

You should use deltaTime to tween the Lerp and multiply it by some amount to speed it up.

Example :

local function Lerp(p0 : any, p1 : any, t : number
	return (1 - t) * p0 + t * p1
end

or

local function Lerp(p0 : any, p1 : any, t : number
	return p0 + (p1 - p0) * t
end

So it should look like this

local function Lerp(p0 : any, p1 : any, t : number
	return p0 + (p1 - p0) * t
end

RunService.Heartbeat:Connect(function(deltaTime : number)
	Lerp(ThingToLerp, TargetValue, deltaTime * speed)
end