Hey,
I want to know how I can determine what direction the player is facing. It should only return one value, either the X direction or the Z direction.
- razor
Hey,
I want to know how I can determine what direction the player is facing. It should only return one value, either the X direction or the Z direction.
- razor
You could be a little more specific with the direction facing with lookvector
player.Character.PrimaryPart.CFrame.LookVector
Basically what it does is, if a player is facing directly X, it will return Vector.new(1,0,0)
Same with Z, Vector3.new(0,0,1)
if the player is facing in between, it will be something like Vector3.new(0.5,0,0.5)
I understand that part, however how do I return only one value? Not a vector3 as it presents more than one value.
You could use orientation,
if the rootpart orientation is Vector3.new(0,0,0), Its facing Z
if its Vector3.new(0,90,0), its facing X
You can’t just return one value, since roblox is a 3d game and not a 1d one, the least amount of variables you can use is just x and z coordinates to actually determine where a player is facing in 3d plane. Unless your trying to find which axes is closest to where the player is facing.
Yeah that’s what I’m trying to achieve. I’m trying to figure out which axis is closest to the player. However, now I’m stuck on which value to prioritize over the other considering that either one could be equivalent in some rare cases.
You can try comparing the angle between the character’s humanoidrootpart and the global axes
and seeing which one has the lowest angle
Made this script in startercharacterscripts
local char = script.Parent
local HumRp: BasePart = char:WaitForChild("HumanoidRootPart")
local Axis = {
["-Z"] = -Vector3.zAxis,
["Z"] = Vector3.zAxis,
["Y"] = Vector3.yAxis,
["-Y"] = -Vector3.yAxis,
["X"] = Vector3.xAxis,
["-X"] = -Vector3.xAxis
}
local function GetAngle(Vec1: Vector3, Vec2: Vector3)
local dot = Vec1:Dot(Vec2)
local angle = math.acos(dot / (Vec1.Magnitude / Vec2.Magnitude))
return math.deg(angle)
end
local function GetClosestAxis(Part: BasePart)
local partLook = Part.CFrame.LookVector
local lowestAngle = 180
local closestAxis
for axis: string, vector: Vector3 in Axis do
local angle = GetAngle(partLook, vector)
if angle < lowestAngle then
lowestAngle = angle
closestAxis = axis
end
end
return closestAxis
end
print(GetClosestAxis(HumRp))