hello, im trying to optimize this script because im using 9 if statement to get the direction where the player is heading
here :
local degree = 0.9
game["Run Service"].RenderStepped:Connect(function()
local MoveDirection = humanoidrootpart.CFrame:VectorToObjectSpace(humanoid.MoveDirection)
if MoveDirection:Dot(Vector3.new(1,0,-1).Unit) > degree then
print("Forwards-Right")
elseif MoveDirection:Dot(Vector3.new(1,0,1).Unit) > degree then
print("Backwards-Right")
elseif MoveDirection:Dot(Vector3.new(-1,0,1).Unit) > degree then
print("Backwards-Left")
elseif MoveDirection:Dot(Vector3.new(-1,0,-1).Unit) > degree then
print("Forwards-Left")
elseif MoveDirection:Dot(Vector3.new(0,0,-1).Unit) > degree then
print("Forwards")
elseif MoveDirection:Dot(Vector3.new(1,0,0).Unit) > degree then
print("Right")
elseif MoveDirection:Dot(Vector3.new(0,0,1).Unit) > degree then
print("Backwards")
elseif MoveDirection:Dot(Vector3.new(-1,0,0).Unit) > degree then
print("Left")
elseif MoveDirection == Vector3.new(0, 0, 0) then
print("None")
end
end)
I already tried with a table but the movedirection isn’t a vector3 with precise number
here
local directiontable = {
{Vector3.new(1,0,-1), "Forwards-Right"},
{Vector3.new(1,0,1), "Backwards-Right"},
{Vector3.new(-1,0,1), "Backwards-Left"},
{Vector3.new(-1,0,-1), "Forwards-Left"},
{Vector3.new(0,0,-1), "Forwards"},
{Vector3.new(1,0,0), "Right"},
{Vector3.new(0,0,1), "Backwards"},
{Vector3.new(-1,0,0), "Left"},
{Vector3.new(0,0,0), "None"},
}
-- these lines are in a renderstepped
local current = table.find(directiontable, MoveDirection)
if current and MoveDirection:Dot(current) > dotThreshold then
print(current)
end