Help needed with getting character to move relative to camera

I have very limited knowledge in Vector3/Position and I have been trying to get this code to move the character relative to Camera. The Camera is also not allowing the normal “freelook” state that you get when holding down Right Click.

  1. What do you want to achieve? A working character moving relative to the camera.

  2. What is the issue? I can’t wrap my head around all of the Vector math.

  3. What solutions have you tried so far? I’ve looked for answers everywhere and non of them seemed to work.

Code below.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:FindFirstChild("Root") or char:WaitForChild("Root")
local anim = char:FindFirstChild("AnimationController")
local input = require(game:GetService("ReplicatedStorage").modules.InputController)
local camera = workspace.CurrentCamera
local walkSpeed = 20
local starterGui = game:GetService("StarterGui")
local uis = game:GetService("UserInputService")

starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)

game:GetService("RunService").RenderStepped:Connect(function()
	camera.Focus = root.CFrame
	uis.MouseIconEnabled = false
end)

repeat
	camera.CameraType = Enum.CameraType.Custom
	wait()
until camera.CameraType == Enum.CameraType.Custom

camera.FieldOfView = 100

local ExampleKeymap = input.RegisterKeymap("ExmapleKeymap", {
	Priority = 1, --Higher priority goes first

	Actions = {
		["ExampleAction1"] = function(Bind, Input)
			print("ExampleAction1 was triggered by " .. tostring(Bind.Trigger))
		end,

		["PressAnyButtonToContinue"] = function(Bind, Input)
			print("Pressed some button on keyboard", Input.KeyCode)
		end,

		["MoveCharacter"] = function(Bind, Input)
			Bind.Active = (Input.UserInputState.Name == "Begin" and true or false)

			local MoveDirection = Vector3.new()

			for _, Bind in pairs(Bind.Keymap.Binds) do
				if (Bind.Action == "MoveCharacter" and Bind.Active) then
					MoveDirection = MoveDirection + Bind.Direction
				end
			end

			print("Moving in", MoveDirection.Magnitude > 0 and MoveDirection.Unit or MoveDirection)
			if MoveDirection.Magnitude > 0 and MoveDirection.Unit or MoveDirection then
				root.BodyForce.Velocity = Vector3.new(camera.CFrame.LookVector.X,0,camera.CFrame.LookVector.Z) * walkSpeed
			end
		end,
	},

	Binds = {
		{
			--Required
			Action = "ExampleAction1", --The name of the action. Must be in the same keymap
			Trigger = Enum.UserInputType.MouseButton1, --What key will trigger the action. Must be `Enum.KeyCode.` or `Enum.UserInputType.`
			TriggerState = "Press", --When should it be triggered. Supports "Down", "Press", "Up" and "Any"

			--Optional
			SinkInput = true, --If set to true, keymaps with a lower priority will not be triggered if this bind gets triggered. Defaults to `false`
			IgnoreGameProcessed = true, --If set to true, the action will be triggered regardless if `gameProcessedEvent` is true, see https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan. Defaults to `false`

			--Any custom bind-specific data can be added after here. Example use case would be for a character controller where there is a single `MoveCharacter` action, and inside each bind there could be a `Direction` value
			Direction = Vector3.new(1, 0, 0),
		},

		{Action = "PressAnyButtonToContinue", Trigger = Enum.UserInputType.Keyboard, TriggerState = "Down"},

		{Action = "MoveCharacter", Trigger = Enum.KeyCode.W, TriggerState = "Any", Direction = Vector3.new(0, 0, -1)},
		{Action = "MoveCharacter", Trigger = Enum.KeyCode.A, TriggerState = "Any", Direction = Vector3.new(-1, 0, 0)},
		{Action = "MoveCharacter", Trigger = Enum.KeyCode.S, TriggerState = "Any", Direction = Vector3.new(0, 0, 1)},
		{Action = "MoveCharacter", Trigger = Enum.KeyCode.D, TriggerState = "Any", Direction = Vector3.new(1, 0, 0)},
	},
}, true)  --If third parameter of `InputController.RegisterKeymap` is true, then they keymap is activated instantly. Otherwise call `InputController.SetKeymapActive("Name", true)`

Thanks in advance.

1 Like

Use the get direction camera walk space function here to make direction relative to camera:

1 Like