How could i know if the player is facing the same direction as the part's direction?

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)
1 Like

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

I cant understand what do you mean my “minus and get the magnitude” i never did that kind of manipulation so by what should i minus them ?

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

1 Like

I am getting a red lign, what am i doing wrong ?

		local HrpLookVector = hrp.CFrame.LookVector
		local PartLookVector = Part.CFrame.LookVector

print({HrpLookVector - PartLookVector}.Magnitude)

Wrong brackets they just need to be normal ()

Try this script instead, @ParadoxAssets sent:

Works really well

1 Like

Couldn’t you just round both numbers and compare?

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.

1 Like

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()

Try this:

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)

This has already been resolved but thank you for your help

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