How do i make Realistic Walking/Running?

You could use inverse kinematics. If you don’t know how to do that, then I suggest you watch this youtube video that I found yesterday.

EDIT: Please check the description of the video as well!

8 Likes

Does it replicate to the server? I tried this method but it doesn’t replicated for other players.

1 Like

it does replicate for me (30 charssssssss)

Where did you place the local script at? I placed it in StarterCharacterScripts, and created the Agility value through a server script that’s placed in ServerScriptService.

i hope this isn’t necroposting or anything, but i’m trying to achieve the same effect for a project i’m working on.

i looked at the script that you gave showing how you coded it and immediately noticed you referenced an ‘agility’ instance in the character. what is that??

also, did you follow a tutorial when writing the code or did you just know how to do it from looking at examples of it? i tried searching on yt for tutorials of it, but all i could find was a video explaining how simple it is to make the base animations; nothing about the actual coding of it. i’m really interested in learning how to do this; any advice would be helpful.

yes, so, agility is an IntValue i made to control humanoid walkspeed, as you can see humanoid WalkSpeed cant go below 0 so i made an IntValue named agility that controls the humanoid WalkSpeed.
you can change the agility to humanoid.WalkSpeed

and no, i didnt follow a tutorial, i just came up with an idea to achieve the effect by using humanoid.MoveDirection and AnimationWeight.

So basically how the script works is by getting the MoveDirection of the humanoid, after we get the movedirection we can play the animation we just made based on the MoveDirection of the humanoid and add some weights to it making it looks like the video above.

2 Likes

thank you so much! you’re a life saver.

Hey just wondering if the code in the topic at the top is the working code or the code from the beginning?

Because if you wouldn’t mind I could use it for my fighting game your choice tho

The code in the topic is working but you need to set it up based on the variables.

Yep understand that such as changing the animation ids and such

1 Like

is there a way I could change the 4 way movement to an 8 way movement system, like back left, back, back right, right, forward right, forward, ect.

–i figured it out

1 Like

So uh. I’m really excited about this stuff. However for some reason it does not replicate for other players. Is there any way to make it replicate for other players? if so, can you tell me what you did to make it replicate for other players? I would greatly appreciate it.

1 Like

you might have to set your animation priority to action and if you have a custom walk animation maybe set the priority to movement.

1 Like

He already found the solution.

2 Likes

you need to set the animation weight to 0.001 when you want it to stop
anything lower causes the animations to stop replicating for some reason

This still replicates oddly. I have been experimenting with the OP’s script, and found a more performant and cleaner looking method. Instead of constantly looping every animation at once and adjusting the weight to 0 to stop the animation, I opted to instead play each animation whenever it’s weight is adjusted, but only if it isn’t currently playing. Here’s the adjusted script:

local function StopAllAnims()
	for i,v in pairs(anim) do
		v:Stop()
	end
end


runService.RenderStepped:Connect(function()
	local movedir = Character.PrimaryPart.CFrame:vectorToObjectSpace(Humanoid.MoveDirection)
	local currentstate = Humanoid:GetState()
	X = movedir.X
	Z = movedir.Z
	local distZ,distZMin = ((Character.PrimaryPart.CFrame.LookVector * 5) - Vector3.new(0,0,Z)).Magnitude,((Character.PrimaryPart.CFrame.LookVector * -5) - Vector3.new(0,0,Z)).Magnitude
	--print("X = "..movedir.X.." Z = "..movedir.Z)
	if currentstate == Enum.HumanoidStateType.Jumping then
		StopAllAnims()
	elseif currentstate == Enum.HumanoidStateType.Freefall then
		StopAllAnims()
	elseif movedir.Magnitude == 0 then
		StopAllAnims()
	else
		if Z == 0 then
			anim["backwalk"]:Stop()
			anim["frontwalk"]:Stop()
			anim["frontrun"]:Stop()
		elseif Z < 0 then
			anim["backwalk"]:Stop()
			if not anim["frontrun"].IsPlaying then
				anim["frontrun"]:Play()
			end
			anim["frontrun"]:AdjustWeight(Z*-1.1)
			if not anim["frontrun"].IsPlaying then
				anim["frontwalk"]:Play()
			end
			anim["frontwalk"]:AdjustWeight(1-((Humanoid.WalkSpeed/10)-1))
		elseif Z > 0 then
			if not anim["backwalk"].IsPlaying then
				anim["backwalk"]:Play()
			end
			anim["backwalk"]:AdjustWeight(Z*1.1)
			anim["frontrun"]:Stop()
			anim["frontwalk"]:Stop()
		end
		if X == 0 then
			anim["rightwalk"]: Stop()
			anim["leftwalk"]: Stop()
		elseif X < 0 and not UIS:IsKeyDown(Enum.KeyCode.W) and not UIS:IsKeyDown(Enum.KeyCode.S) then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-1.2)
			anim["leftwalk"]:Stop()
			--print(X*-1.2)
		elseif X > 0 and not UIS:IsKeyDown(Enum.KeyCode.W) and not UIS:IsKeyDown(Enum.KeyCode.S) then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*1.2)
			--print(X*1.2)
		elseif X > 0 and Z > 0 then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*0.4)
		elseif X > 0 and Z < 0 then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*0.4)
		elseif X < 0 and Z > 0 then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-0.4)
			anim["leftwalk"]:Stop()
		elseif X < 0 and Z < 0 then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-0.4)
			anim["leftwalk"]:Stop()
		end
	end
end)
	

This script replicates 100% correctly and doesn’t suffer the animation weight problem. I highly recommend this method to the OP’s.

5 Likes

truly a step up from my version which just gets rid of a lot of unnecessary/weird code

game:GetService("RunService").Heartbeat:Connect(function()
	for i,v in pairs(anim) do
		v:AdjustSpeed(humanoid.WalkSpeed/16)
	end
	local movedir = character.HumanoidRootPart.CFrame:vectorToObjectSpace(humanoid.MoveDirection)
	local currentstate = humanoid:GetState()
	X = movedir.X
	Z = movedir.Z
	local distZ,distZMin = ((character.PrimaryPart.CFrame.LookVector * 5) - Vector3.new(0,0,Z)).Magnitude,((character.PrimaryPart.CFrame.LookVector * -5) - Vector3.new(0,0,Z)).Magnitude
	if Z == 0 then
		anim["backwalk"]:AdjustWeight(0.0001)
		anim["frontwalk"]:AdjustWeight(0.0001)
	elseif Z < 0 then
		anim["backwalk"]:AdjustWeight(0.0001)
		anim["frontwalk"]:AdjustWeight(-Z-0.0001)
	elseif Z > 0 then
		anim["backwalk"]:AdjustWeight(Z+0.0001)
		anim["frontwalk"]:AdjustWeight(0.0001)
	end
	if X == 0 then
		anim["leftwalk"]:AdjustWeight(0.0001)
		anim["rightwalk"]:AdjustWeight(0.0001)
	elseif X < 0 and Z == 0 then
		anim["leftwalk"]:AdjustWeight(-X-0.0001)
		anim["rightwalk"]:AdjustWeight(0.0001)
	elseif X > 0 and Z == 0 then
		anim["leftwalk"]:AdjustWeight(0.0001)
		anim["rightwalk"]:AdjustWeight(X+0.0001)
	elseif X > 0 and Z > 0 then
		anim["leftwalk"]:AdjustWeight(0.0001)
		anim["rightwalk"]:AdjustWeight(X+0.0001)
	elseif X > 0 and Z < 0 then
		anim["leftwalk"]:AdjustWeight(0.0001)
		anim["rightwalk"]:AdjustWeight(X+0.0001)
	elseif X < 0 and Z > 0 then
		anim["leftwalk"]:AdjustWeight(-X-0.0001)
		anim["rightwalk"]:AdjustWeight(0.0001)
	elseif X < 0 and Z < 0 then
		anim["leftwalk"]:AdjustWeight(-X-0.0001)
		anim["rightwalk"]:AdjustWeight(0.0001)
	end
		anim["idle"]:AdjustWeight(.2)
end)
2 Likes

So uh… Not trying to be dumb or anything but where would u place the script?
I’m still kinda new to scripting

Place this script in StarterPlayer > StarterCharacterScripts

2 Likes

i also made a version that dosent use key inputs or camera to detect in which direction the player is headed, u can find the post here: 8 Way Directional Walking System Open Sourced

3 Likes