How to find relative direction to camera without using playermodule

Hi,

Long time ago I went through this problem and my solution was to get the playermodule, get the controls and get the move vector of the player from controls like PlayerModule:GetControls:GetMoveVector… Don’t ask me why I don’t want to use the player module…

But now I want to find out how to find the relative direction without using robloxs playermodule…

Here’s my current script and my attempt:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Knit = require(ReplicatedStorage.Packages.Knit)
local ContextActionUtility = require(ReplicatedStorage.Sources.Utility.ContextActionUtility)
local waitForChildWhichIsA = require(ReplicatedStorage.Sources.Utility.waitForChildWhichIsA)
-- Variables
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
-- Controller
local Movement = Knit.CreateController{
	Name = "Movement"
}

local Directions = {
	[Vector3.new(0, 0, 0)] = "Not Moving";
	[Vector3.new(0, 0, -1)] = "Foward";
	[Vector3.new(0, 0, 1)] = "Backward";
	[Vector3.new(-1, 0, 0)] = "Left";
	[Vector3.new(1, 0, 0)] = "Right";
	[Vector3.new(-1, 0, -1)] = "FowardLeft";
	[Vector3.new(1, 0, -1)] = "FowardRight";
	[Vector3.new(-1, 0, 1)] = "BackwardLeft";
	[Vector3.new(1, 0, 1)] = "BackwardRight";
}

function onDirection()
	local RelativeDirection = workspace.CurrentCamera.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
	
	local Direction = Vector3.new(
		math.round(RelativeDirection.X),
		math.round(RelativeDirection.Y),
		math.round(RelativeDirection.Z)
	)
	
	print(Directions[Direction], Directions[RelativeDirection])
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(onDirection)

return Movement
1 Like

I still need help with this :frowning_face:… SOMEONE HELP ME PLEASE!!!

I’m not sure if this is what you were looking for, but give this function a try.

local DIRECTION_VALUES = {
	["Not Moving"] = {0, 0},
	
	["BackwardRight"] = {-1, 1},
	["BackwardLeft"] = {-1, -1},
	
	["ForwardRight"] = {1, 1},
	["ForwardLeft"] = {1, -1},
	
	["Backward"] = {-1, 0},
	["Forward"] = {1, 0},
	
	
	["Right"] = {0, 1},
	["Left"] = {0, -1}
}

local function calculateRelativeDirection(cameraInstance: Camera, humanoid: Humanoid)
	local cameraRightVector = cameraInstance.CFrame.RightVector
	local cameraLookVector = cameraInstance.CFrame.LookVector
	local moveDirection = humanoid.MoveDirection
	
	local lookDot, rightDot = math.round(moveDirection:Dot(cameraLookVector)), math.round(moveDirection:Dot(cameraRightVector))
	
	for movementStatus: string, vectorData: {number} in DIRECTION_VALUES do
		if vectorData[1] ~= lookDot or vectorData[2] ~= rightDot then
			continue
		end
		
		return movementStatus
	end
	
	return "Not Moving"
end
1 Like

This works perfectly, but can I ask why you put “return “Not Moving”” instead of return?

So that it’ll be the default return case incase something breaks. (If math.round returns an unexpected value)

1 Like

Wow that makes sense, thanks for the explanation!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.