How can I make a character move to the direction it is facing?

I want my character to move to what ever direction its HumanoidRootPart is facing
Right now it just moves along the x-axis.
Here is the script I’ve made

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local moveValue = 0

local function OnForward(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		moveValue = 1
	elseif inputState == Enum.UserInputState.End then
		moveValue = 0
	end
end


local function move()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		player.Character.Humanoid:Move(Vector3.new(moveValue,0,0), false)
	end
end
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, move)

ContextActionService:BindAction("Forward", OnForward, true, "w")

I am not sure if I should use PrimaryPart.CFrame instead and maybe LookVector or something?

4 Likes

I managed to find out one solution for this myself. I used sin and cos to convert the y orientation of HumanoidRootPart to Vector3 x,z values.
Here is how

local function OnForward(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		rotation = RootPart.Orientation.Y
		Xdirection = math.sin(0.0174532925*rotation)*-1
		Zdirection = math.cos(0.0174532925*rotation)*-1
	elseif inputState == Enum.UserInputState.End then
		Xdirection = 0
		Zdirection = 0
	end
end

I’m not sure if this is a proper way to do it but for now it seems to work.

5 Likes