Any way to get player movement?

In my Lua script, I need to make it so it changes your material whenever you move. However, my current method, changes the material when you press any movement-related buttons and is not very good.


	if key == IN_FORWARD or key == IN_BACK or key == IN_MOVELEFT or key == IN_MOVERIGHT or key == IN_JUMP then
		self:SetMaterial( "models/effects/comball_tape" )
	else 
		self:SetMaterial( "sprites/heatwave" )
	end

Is there anyway I can do “if ply:IsMoving() then” or anything like that? I’ve checked the wikis and could not find anything.

Thanks.

You could get the velocity of the player.
**[Entity.GetVelocity

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/page/Entity/GetVelocity)**

And then get the length of that vector.
**[Vector.Length

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/page/Vector/Length)**

And then do checks with that number.


if ( player:GetVelocity():Length() > 0 ) then /*moving*/
else /* not moving */ end


Do “player:GetVelocity():Length() > 5” since source can act up a bit.


	if ( self:GetVelocity():Length() > 5 ) then
		self:SetMaterial( "models/effects/comball_tape" )
	else 
		self:SetMaterial( "sprites/heatwave" )
	end

This doesn’t seem to work. If I stand still and then hold one of W, S, A, or D, I’ve still got heatwave material. If I hold more than one button, then it sets the more visible material.

Might I ask where are you calling this? In a Think hook?

I was accidentally calling it in an onkeypress. Put it in a think hook and it worked fine. Thanks.