Making 8-Directional Movement?

I am trying to make an 8 Directional Script, would be pretty simple by playing animations, checking the Humanoid’s MoveDirection,

--[[

Move Direction:
(0, 0, -1) - Forward
(0, 0, 1) - Backward
(-1, 0, 0) - Left
(1, 0, 0) - Right

(-1, 0, -1) - Forward Left
(1, 0, -1) - Forward Right
(-1, 0, 1) - Backward Left
(1, 0, 1) - Backward Right

]]

but the problem is, I can’t actually check the player’s moveDirection properly, sometimes it goes .9, .7 etc

1 Like

local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
local Controls = PlayerModule:GetControls()

print(Controls:GetMoveVector())

Controls returns a move direction vector which will never be .9 .7 etc. Test it out

4 Likes

Thanks, I tried it and it worked!
I’ve got a problem tho, you see, when the player isn’t shiftlocking, the MoveDirection is the same
(if the Player moves backwards with S, if they don’t have shift lock the player will just turn to face backwards)
so if I try to say “if player moves back”, if the player doesn’t have shift lock, their character would face the camera and their legs would go backward

TL;DR try using the CFrame functions like :ToObjectSpace, hard to explain sry.

I do not understand your explanation fully, however I’m assuming it’s an issue to do with the control module being relative to world and such.

You can do this to make it relative to camera, or modify it to be relative to the humanoid root part:

        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

Example of ToObjectSpace for humanoid root part to create a tilt effect in direction of movement.

yeah, I don’t really know how to explain this.
so if you have shiftlock and move backwards, your character looks forward and moves backwards
but if you don’t have shiftlock, you move backwards and your character rotates. While this isn’t a problem on it’s own, no matter if you use shiftlock or not, pressing “S” and going backwards will result (0, 0, 1)

If I say “(0,0,1) → backwards animation” then it would look weird if the player wouldn’t use shift lock

EDIT: If you look closely, it shows 0,0,1; but the player moves forward, not backward.

EDIT 2: I’m thinking of 3 possibilities

  1. Make the animations work only if the player is shiftlocking
  2. Constantly turn the player with the camera as if it’s shiftlocking (but having a cursor not locked in place)
  3. fix the existing script, but I don’t really know how

You should be able to solve that using this, make sure to get the humanoid root part to make the vector relative to that.

local vector = Controls:GetMoveVector()
        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
print(clampedMoveDirectionRelativeToCamera) --use this direction vector

This results in constant (0,0,0) results to me, am I doing something wrong?

--[[

Move Direction:
(0, 0, -1) - Forward
(0, 0, 1) - Backward
(-1, 0, 0) - Left
(1, 0, 0) - Right

(-1, 0, -1) - Forward Left
(1, 0, -1) - Forward Right
(-1, 0, 1) - Backward Left
(1, 0, 1) - Backward Right

]]
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = script.Parent or Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
local Controls = PlayerModule:GetControls()

local vector = Controls:GetMoveVector()
local movementMagnitude = math.clamp(vector.Magnitude, 0, 1)
local moveDirection = HRP.CFrame:VectorToWorldSpace(vector)*Vector3.new(1,0,1)
if moveDirection.Magnitude > 0.01 then
	moveDirection = moveDirection.Unit
end
local Move = moveDirection*movementMagnitude

while wait(0.5) do
	print(Move) --use this direction vector
end
1 Like

Yeah you need to update the move vector

--[[

Move Direction:
(0, 0, -1) - Forward
(0, 0, 1) - Backward
(-1, 0, 0) - Left
(1, 0, 0) - Right

(-1, 0, -1) - Forward Left
(1, 0, -1) - Forward Right
(-1, 0, 1) - Backward Left
(1, 0, 1) - Backward Right

]]
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = script.Parent or Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
local Controls = PlayerModule:GetControls()


while wait(0.5) do

local vector = Controls:GetMoveVector()
local movementMagnitude = math.clamp(vector.Magnitude, 0, 1)
local moveDirection = HRP.CFrame:VectorToWorldSpace(vector)*Vector3.new(1,0,1)
--Also try replacing it with camera cframe if it doesn't work
--local moveDirection = camera.CFrame:VectorToWorldSpace(vector)*Vector3.new(1,0,1)

if moveDirection.Magnitude > 0.01 then
	moveDirection = moveDirection.Unit
end
local Move = moveDirection*movementMagnitude
	print(Move) --use this direction vector
end
3 Likes

YES! IT worked. And yes, the camera.CFrame worked instead of HRP

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