Help with Dynamic movement

I want to create a dyanimc movement system smillar to ones in games like jailbreak and madcity

heres a sample

But unfortunately while recreating the system I stumbled apoun a glitch
when ever I crouch I proagmed the realistic head movment to stop when crouching!
unfortunately the postion of the head/torso dose not revert to normal creating a jaring effect.

Video example

Ive attempeted to change the headver, headvor and other factors to fix my problem
I even tried to create a sperate script to no avail.

Heres the code

local player = game.Players.LocalPlayer
local character = player.Character
local UIS = game:GetService("UserInputService")

local Plr = character:WaitForChild("Humanoid")

local crawl = character.Humanoid:LoadAnimation(script:WaitForChild("Crawl"))

-- Services
local runService = game:GetService("RunService")
local playerService = game:GetService("Players")

-- Variables
local headHorFactor = 1
local headVerFactor = .6
local bodyHorFactor = .5
local bodyVerFactor = .4
local updateSpeed = .5 / 2

local plr = playerService.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local isR6 = hum.RigType == Enum.HumanoidRigType.R6
local head = char.Head
local root = char.HumanoidRootPart
local torso = isR6 and char.Torso or char.UpperTorso
local neck = isR6 and torso.Neck or head.Neck
local waist = not isR6 and torso.Waist

local neckC0 = neck.C0
local waistC0 = not isR6 and waist.C0



local isCrawling = false

Plr.Running:Connect(function(speed)
	if speed > 0 then
		crawl:AdjustSpeed(1)
	else
		crawl:AdjustSpeed(0)
	end
end)



UIS.InputBegan:Connect(function(input, isTyping)
	
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if not isCrawling then
				Plr.JumpHeight = 7.2
				character.Humanoid.WalkSpeed = 22
			end
			    
			
			
		
		elseif input.KeyCode == Enum.KeyCode.C then
			if isCrawling then
				crawl:Stop()
				isCrawling = false
				character.Humanoid.WalkSpeed = 16
				Plr.JumpHeight = 7.2
				
			else
				
				Plr.JumpHeight = 0
				crawl:Play()
				isCrawling = true
				
				character.Humanoid.WalkSpeed = 10
			end
		end
		
		
		
	
		
		
		
		
		
	end
	
	
end)

UIS.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if not isCrawling then
				character.Humanoid.WalkSpeed = 16
				Plr.JumpHeight = 7.2
			end
		end
	end
end)

neck.MaxVelocity = 1/3

runService.RenderStepped:Connect(function ()
	if not isCrawling then
		-- Check if every required body part exists and whether the CurrentCamera's CameraSubject is the Humanoid
		if torso and head and ((isR6 and neck) or (neck and waist)) and cam.CameraSubject == hum then

			local camCF = cam.CFrame
			local headCF = head.CFrame

			local torsoLV = torso.CFrame.lookVector

			local dist = (headCF.p - camCF.p).magnitude
			local diff = headCF.Y - camCF.Y

			local asinDiffDist = math.asin(diff / dist)
			local whateverThisDoes = ((headCF.p - camCF.p).Unit:Cross(torsoLV)).Y

			if isR6 then
				neck.C0 = neck.C0:lerp(neckC0 * CFrame.Angles(-1 * asinDiffDist * headVerFactor, 0, -1 * whateverThisDoes * headHorFactor), updateSpeed)
			else
				neck.C0 = neck.C0:lerp(neckC0 * CFrame.Angles(asinDiffDist * headVerFactor, -1 * whateverThisDoes * headHorFactor, 0), updateSpeed)
				waist.C0 = waist.C0:lerp(waistC0 * CFrame.Angles(asinDiffDist * bodyVerFactor, -1 * whateverThisDoes * bodyHorFactor, 0), updateSpeed)
			end

		end
	end
end)

all I need is a few lines of code to reset the torso/head to its deafult position!
I’d be really tahnkful if someone could help out thanks a million! :smile:

1 Like

This should fix it

local player = game.Players.LocalPlayer
local character = player.Character
local UIS = game:GetService("UserInputService")

local Plr = character:WaitForChild("Humanoid")

local crawl = character.Humanoid:LoadAnimation(script:WaitForChild("Crawl"))

-- Services
local runService = game:GetService("RunService")
local playerService = game:GetService("Players")

-- Variables
local headHorFactor = 1
local headVerFactor = .6
local bodyHorFactor = .5
local bodyVerFactor = .4
local updateSpeed = .5 / 2

local plr = playerService.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")
local isR6 = hum.RigType == Enum.HumanoidRigType.R6
local head = char.Head
local root = char.HumanoidRootPart
local torso = isR6 and char.Torso or char.UpperTorso
local neck = isR6 and torso.Neck or head.Neck
local waist = not isR6 and torso.Waist

local neckC0 = neck.C0
local waistC0 = not isR6 and waist.C0

local isCrawling = false

Plr.Running:Connect(function(speed)
	if speed > 0 then
		crawl:AdjustSpeed(1)
	else
		crawl:AdjustSpeed(0)
	end
end)

UIS.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if not isCrawling then
				Plr.JumpHeight = 7.2
				character.Humanoid.WalkSpeed = 22
			end
		elseif input.KeyCode == Enum.KeyCode.C then
			if isCrawling then
				-- Stop crawling and reset head/neck positions
				crawl:Stop()
				isCrawling = false
				character.Humanoid.WalkSpeed = 16
				Plr.JumpHeight = 7.2

				-- Reset neck and waist positions
				neck.C0 = neckC0
				if waist then
					waist.C0 = waistC0
				end
			else
				-- Start crawling
				Plr.JumpHeight = 0
				crawl:Play()
				isCrawling = true
				character.Humanoid.WalkSpeed = 10
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if not isCrawling then
				character.Humanoid.WalkSpeed = 16
				Plr.JumpHeight = 7.2
			end
		end
	end
end)

neck.MaxVelocity = 1/3

runService.RenderStepped:Connect(function()
	if not isCrawling then
		-- Check if every required body part exists and whether the CurrentCamera's CameraSubject is the Humanoid
		if torso and head and ((isR6 and neck) or (neck and waist)) and cam.CameraSubject == hum then

			local camCF = cam.CFrame
			local headCF = head.CFrame

			local torsoLV = torso.CFrame.lookVector

			local dist = (headCF.p - camCF.p).magnitude
			local diff = headCF.Y - camCF.Y

			local asinDiffDist = math.asin(diff / dist)
			local whateverThisDoes = ((headCF.p - camCF.p).Unit:Cross(torsoLV)).Y

			if isR6 then
				neck.C0 = neck.C0:lerp(neckC0 * CFrame.Angles(-1 * asinDiffDist * headVerFactor, 0, -1 * whateverThisDoes * headHorFactor), updateSpeed)
			else
				neck.C0 = neck.C0:lerp(neckC0 * CFrame.Angles(asinDiffDist * headVerFactor, -1 * whateverThisDoes * headHorFactor, 0), updateSpeed)
				waist.C0 = waist.C0:lerp(waistC0 * CFrame.Angles(asinDiffDist * bodyVerFactor, -1 * whateverThisDoes * bodyHorFactor, 0), updateSpeed)
			end

		end
	else
		-- Keep neck and waist static during crawling
		neck.C0 = neckC0
		if waist then
			waist.C0 = waistC0
		end
	end
end)

Tell me if you have any problems

1 Like

Thanks it works like a charm! :smile:

1 Like

I decided to change the animation to something more polished and thats when I experienced a few strange glitches. when I imported the animation id straight into the animation variable I began to have some trouble the characters would start walking odd not playing the animation correctly
but for some strange reason the orginal worked fine I am not sure if this has to do with the animation editor.

heres a video example

During the second attempt I deleted all the keyframes of the orginal animation and pasted
the keyframes of the new animation I created that when he started turining 90 degrees upward everytime the animation looped. Next I tried multiple diffrent ways of fix it at first I thought perhaps it has something to do with the hip height( which didn’t fix the issue ) then I tought it might have been something in the code, but the code has nothing to do with animation, next I wondered if the issue had also been responseable for the reason the hands and feet shake when the animation plays but while rescreaching I found nothing.

and now I wonder if you know whats going on :sweat_smile:

1 Like

Uh can you send me the place file, ill try my best to fix it :slight_smile:

Heres the place file! :grin:

Movement.rbxl (92.5 KB)

Okay, i made the dummy play your crawl animation and its having the same issue, so its not the code, its the animation

Thats strange I made that animation do you know how to fix it? or at least whats causing the issue. :thinking:

For whatever reason instead of starting from scrath modifing the orginal fixed the issue

video example

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.