PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

MoveDirection

Humanoid

Vector3

A visualization of the MoveDirection property.

MoveDirection is a read-only property that describes the direction a humanoid is walking in, as a unit vector along the X/Z axis. The direction is described in world space.


Code Samples


Walking Camera Bobble Effect

This LocalScript makes the camera bobble as the player’s character walks around, utilizing both the Humanoid’s CameraOffset and MoveDirection.
It should be parented inside of the StarterCharacterScripts so that it is distributed into a player’s character as expected.

local RunService = game:GetService("RunService")

local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")

local function updateBobbleEffect()
	local now = tick()
	if humanoid.MoveDirection.Magnitude > 0 then -- Are we walking?
		local velocity = humanoid.RootPart.Velocity
		local bobble_X = math.cos(now * 9) / 5
		local bobble_Y = math.abs(math.sin(now * 12)) / 5
		
		local bobble = Vector3.new(bobble_X,bobble_Y,0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
		humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble,.25)
	else
		-- Scale down the CameraOffset so that it shifts back to its regular position.
		humanoid.CameraOffset = humanoid.CameraOffset * 0.75
	end
end

-- Update the effect on every single frame.
RunService.RenderStepped:Connect(updateBobbleEffect)