Player movement relative to the camera

What I’m trying to do: I’m trying to have the player move relative to the camera’s perspective.

I’m currently giving the player(a sphere) movement with a VectorForce.

This is the base parts of the code I’m using to apply the force from the players input

local d = Vector3.new(forward-backward, 0, right-left) --Where forward,backward,right, and left are either 1 or 0 depending on keys pressed
local force =  d*PLAYER_SPEED_FORCE
player.Character.HumanoidRootPart.VectorForce.Force = force

But here’s also the full script incase you are curious.

Code
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local FRICTION_COE = 15
local PLAYER_SPEED_FORCE = 600
local FORWARD_KEY = Enum.KeyCode.W
local BACKWARD_KEY = Enum.KeyCode.S
local LEFT_KEY = Enum.KeyCode.A
local RIGHT_KEY = Enum.KeyCode.D

local forwardForceVector

local forward = 0
local backward = 0
local left = 0
local right = 0

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

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

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

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

local function move()
	local d = Vector3.new(forward-backward, 0, right-left)
			
	local force =  d*PLAYER_SPEED_FORCE
	
	if d.x~=d.x then
		d = Vector3.zero
	end
	
	local velD = player.Character.HumanoidRootPart.AssemblyLinearVelocity.Unit
	
	if velD.x~=velD.x then
		velD = Vector3.zero
	end
	
	local friction = velD * (FRICTION_COE * player.Character.HumanoidRootPart.AssemblyLinearVelocity.magnitude) * -1
	
	local total = force + friction
	print("Force   : " .. tostring(force))
	print("Friction: " .. tostring(friction))
	print("Total   : " .. tostring(total))
	player.Character.HumanoidRootPart.VectorForce.Force = total
end

RunService.RenderStepped:Connect(function()
	if player.Character and player.Character.HumanoidRootPart then
		move()
	end
end)



ContextActionService:BindAction("MoveLeft", onLeft, false, LEFT_KEY)
ContextActionService:BindAction("MoveRight", onRight, false, RIGHT_KEY)
ContextActionService:BindAction("MoveForward", onForward, false, FORWARD_KEY)
ContextActionService:BindAction("MoveBackward", onBackward, false, BACKWARD_KEY)

However, this only is relative to world space, and I am trying to make it relative to the player’s camera. I’ve looked up documentation of Vector3’s and CFrame’s but I couldn’t figure out what I needed.

Camera which looks at the player like so, lines indicating where the ball ‘should’ be rolling if it were relative to the camera.

If anyone would like to help out with this, Thanks in advance!

To make it relative to camera space you can just use CFrame multiplication.

local d = camera.CFrame*Vector3.new(forward-backward

This changes the axis from world space axis to camera axis.

1 Like