Walking direction is not relative to the camera

Hi I am working on 8 direction walking and for some reason my script doesn’t work relative to the camera.

Here’s the visual problem.

You can see in the video, it’s printing front, but when i turn my camera and go front again from another perspective, it prints left.

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ExtraFunctions = require(ReplicatedStorage.Modules.ExtraFunctions)
local MathAddons = require(ReplicatedStorage.Modules.MathAddons)

local LocalPlayer = Players.LocalPlayer
if not LocalPlayer then
	Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
	LocalPlayer = Players.LocalPlayer
end

local PlayerScripts = ExtraFunctions:WaitForChildWhichIsA(LocalPlayer, "PlayerScripts") :: PlayerScripts
local PlayerModule = require(PlayerScripts:WaitForChild("PlayerModule")) :: any

local Controls = PlayerModule:GetControls()

local Humanoid: Humanoid?
--local Character = script.Parent or LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
--local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local PlayerAnimations = ReplicatedStorage.Animations.PlayerAnimations

local LoadedAnimations = {}
local WalkAnimations = {
	PlayerAnimations.WalkBack;
	PlayerAnimations.WalkFront;
}

local Camera = workspace.CurrentCamera

while wait(1) do
	local Vector = Controls:GetMoveVector()
	local movementMagnitude = math.clamp(Vector.Magnitude, 0, 1)
	local moveDirection = Camera.CFrame:VectorToWorldSpace(Vector)*Vector3.new(1,0,1)

	if moveDirection.Magnitude > 0.01 then
		moveDirection = moveDirection.Unit
	end

	local clampedMoveDirectionRelativeToCamera = moveDirection*movementMagnitude

	local Move = moveDirection*movementMagnitude

	local Direction = Vector3.new(
		MathAddons.round(Move.X),
		MathAddons.round(Move.Y),
		MathAddons.round(Move.Z)
	)
	
	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";
	}
	
	print(Directions[Direction]," | ",Direction)
end

1 Like

Here’s some more footage

https://gyazo.com/4e487866082e86ff8b1fe470bf27a5db

1 Like

After a couple hours I found the solution. I just used the vector3 from Controls:GetMoveVector()

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local ExtraFunctions = require(ReplicatedStorage.Modules.ExtraFunctions)
local MathAddons = require(ReplicatedStorage.Modules.MathAddons)

local LocalPlayer = Players.LocalPlayer
if not LocalPlayer then
	Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
	LocalPlayer = Players.LocalPlayer
end

local PlayerScripts = ExtraFunctions:WaitForChildWhichIsA(LocalPlayer, "PlayerScripts") :: PlayerScripts
local PlayerModule = require(PlayerScripts:WaitForChild("PlayerModule")) :: any

local Controls = PlayerModule:GetControls()

local Humanoid: Humanoid?
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local PlayerAnimations = ReplicatedStorage.Animations.PlayerAnimations

local LoadedAnimations = {}
local WalkAnimations = {
	PlayerAnimations.WalkBack;
	PlayerAnimations.WalkFront;
}

while wait(0.5) do
	local Camera = workspace.CurrentCamera
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local Vector = Controls:GetMoveVector()
	
	-- Most of these stuff are useless.
	local movementMagnitude = math.clamp(Vector.Magnitude, 0, 1)
	local moveDirection = HumanoidRootPart.CFrame:VectorToWorldSpace(Vector)*Vector3.new(1,0,1)

	if moveDirection.Magnitude > 0.01 then
		moveDirection = moveDirection.Unit
	end

	local clampedMoveDirectionRelativeToCamera = moveDirection*movementMagnitude

	local Move = moveDirection*movementMagnitude

	--local Direction = Vector3.new(
	--	MathAddons.round(Move.X),
	--	MathAddons.round(Move.Y),
	--	MathAddons.round(Move.Z)
	--)

	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";
	}

	print(Directions[Vector]," | ",Vector)
end
4 Likes

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