How do i determine what direction the player is facing?

i’m working on a 2D fighting game, and i want to figure out what direction a player’s facing relative to the camera

here’s a visualizer:

thanks in advance.

1 Like

Maybe lookvector is the best, use it in relation to the player’s head

Head.CFrame.LookVector              -- Front
-Head.CFrame.LookVector             -- Back

if it refers to the character, if not, maybe the answer below will work and multiply it by a number if you used RayCast or similar to detect something.

use Camera.LookVector

local camera = workspace.CurrentCamera
local direction = camera.CFrame.LookVector

Also, this would be a local script, you would have to pass the direction through a remoteevent if you want to use it in a server script

Use the CFrame.RightVector as a determiner of left or right., the negative is left. Your opponent is either left or right. If the depth position is fixed, use the fixed depth position, not character position(unless you’re going for rotating around a center or something). The client needs to track two things:

  1. Your own character’s position
  2. Your opponent’s position

You can calculate their differences in the moving space. Not Y which is for the height. If you calculate their difference from one to another, the value will either:

  1. Be positive
  2. Be negative

In either cases, depending how you calculated it, you will find yourself either left or right of the opponent or vice versa.

Also for the current direction that the player’s facing. The head may be unreliable and the only thing that is more fixed than the other body parts is the HumanoidRootPart. You may use its LookVector.

Use LookVector.

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local FrontCamera = Camera.CFrame.LookVector

Camera.CFrame = Camera.CFrame * FrontCamera * CFrame.new(0, 0, 5)

Or use Pivot. This returns a CFrame on the side where you are viewing the model/part, usually used to move a model but also works with part.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")

local PivotHead = Head:GetPivot()

Character:PivotTo(PivotHead * CFram.new(0, 0, 10))

With this, the player should move 10 studs on the side of the player’s model that is looking at the “Head” of the player’s model.

1 Like