Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how do I detect how fast a player changes direction?

Asked by 3 years ago

I want to make the player model tilt towards the direction the player is moving (the quicker it changes direction, the more the player model tilts) and I don't really know how to do either (detect change in direction and tilt player model) could anyone help? Cheers!

1 answer

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
3 years ago
Edited 3 years ago

you can simply use the Y component of the HumanoidRootPart's RotVelocity to find the speed the player is rotating at, then base the amount of tilt on that number

to actually apply the tilt, you can change the Transform of the character's root joint

where that joint is located and what it's called depends on whether your game is r6 or r15

if you want it to appear smooth you can repeatedly lerp the Transform instead of just constantly setting it

-- LocalScript in StarterCharacterScripts
local RunService = game:GetService("RunService")

local char = script.Parent
local rootPart = char:WaitForChild("HumanoidRootPart")
local rootJoint = rootPart:WaitForChild("RootJoint") -- change this to char:WaitForChild("LowerTorso"):WaitForChild("Root") if your game uses R15
-- if your game supports both you'll have to check to see which the character uses with Humanoid.RigType

local transform = CFrame.new()
local originalC0 = rootJoint.C0

rootJoint:GetPropertyChangedSignal("C0"):Connect(function() -- account for changes to the C0 made by actual roblox scripts
    if rootJoint.C0 ~= originalC0 * transform then
        originalC0 = rootJoint.C0
    end
end)

RunService.RenderStepped:Connect(function(delta)
    local tilt = math.clamp(rootPart.RotVelocity.Y * 4, -45, 45)
    transform = transform:Lerp(CFrame.Angles(0, math.rad(tilt), 0), 0.25 * delta * 60) -- you can change 0.25 here to whatever you want
    rootJoint.C0 = originalC0 * transform
end)

if you want other people to be able to see tilt as well, you can copy and paste the same code into a server script, but with RenderStepped replaced with Heartbeat

0
Cant get it to work. Am i doing anything wrong? Scrufflord 12 — 3y
0
is there an error Elyzzia 1294 — 3y
0
OK i figured out what was wrong, apparently the character doesn't like it when you change its Transform and just does. literally nothing Elyzzia 1294 — 3y
0
i updated the code to actually. you know, work Elyzzia 1294 — 3y
Ad

Answer this question