I tried something but its a high range of numbers so its hard to the player to find the right angle
what i mean by high range of numbers : 11:25:13.626 0.6940251588821411, -0.001314125140197575, -0.7199495434761047 - Server - Script:5
wait(2)
local Part = script.Parent
Part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
local hrp = Hit.Parent:FindFirstChild("HumanoidRootPart")
if hrp.CFrame.LookVector == Part.CFrame.LookVector then
print("same direction")
end
end
end)
You could minus them then get the magnitude. I can’t remember if it would be smaller the closer of larger but just some trial and error could find that
Minus the 2 look vectors, put them ain brackets and put .Magnitude after. This returns are value from -1 to 1 you can then just compare of it’s within a certain range e.g greater than 0.5 but less than 0.75
local function isFacingSameDirection(player, part)
-- Ensure the player has a character and HumanoidRootPart
if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then
return false
end
-- Get the HumanoidRootPart of the player
local humanoidRootPart = player.Character.HumanoidRootPart
-- Get the forward (look) vectors of the HumanoidRootPart and the part
local playerLookVector = humanoidRootPart.CFrame.LookVector
local partLookVector = part.CFrame.LookVector
-- Compare the look vectors
local dotProduct = playerLookVector:Dot(partLookVector)
-- Check if the dot product is close to 1 (facing the same direction)
local threshold = 0.99
return dotProduct >= threshold
end
This is unreliable, you will most likely get a floating point error at some point.
Getting the same rotation as a part already placed with a script is nearly impossible.
The character is constantly moving due to the idle animation, (you need a threshold)
When comparing two Vector3s you want to use the fuzzy equals function:
local epsilon = 0.1 -- make this number bigger or smaller depending on how similar you want the two vectors to be
if hrp.CFrame.LookVector:FuzzyEq(Part.CFrame.LookVector, epsilon) then
print("same direction")
end
This code checks that all components of the humanoid root part’s look vector are within 0.1 studs of the other part’s look vector. “Epsilon” is the threshold. This fixes the problem of comparing two vectors that contain really long decimals.
I never used functions before so how do i connect the part variable to the function ?
this is what i did
local player = game.Players.LocalPlayer
local part = script.Parent
local function isFacingSameDirection(player)
-- Ensure the player has a character and HumanoidRootPart
if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then
return false
end
-- Get the HumanoidRootPart of the player
local humanoidRootPart = player.Character.HumanoidRootPart
-- Get the forward (look) vectors of the HumanoidRootPart and the part
local playerLookVector = humanoidRootPart.CFrame.LookVector
local partLookVector = part.CFrame.LookVector
-- Compare the look vectors
local dotProduct = playerLookVector:Dot(partLookVector)
-- Check if the dot product is close to 1 (facing the same direction)
local threshold = 0.99
return dotProduct >= threshold
end
wait(2)
isFacingSameDirection()
local part = script.Parent
local function facingSameDirection(char)
local roundedcharlv = Vector3.new(math.round(char.HumanoidRootPart.CFrame.LookVector.X), math.round(char.HumanoidRootPart.CFrame.LookVector.Y), math.round(char.HumanoidRootPart.CFrame.LookVector.Z))
local roundedpartlv = Vector3.new(math.round(part.CFrame.LookVector.X), math.round(part.CFrame.LookVector.Y), math.round(part.CFrame.LookVector.Z))
return roundedcharlv == roundedpartlv
end
part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
if facingSameDirection(Hit.Parent) == true then
print("same direction")
end
end
end)