How would I detect if a part is infront or behind the player?

could you explain

math.acos

to me please?

1 Like

I tested this code out and while it does return true if the player’s character is in front of the part,
it would return false sometimes if
The player was in front of the part and also facing the part’s front
And also return true if the player’c character was directly behind the part but facing the complete opposite of the part’s lookvector

1 Like

math.acos is the inverse function of cosine. If you dont know what cosine is, there are many videos about it. Anyways, what it does (in my code) is take the angle of the Dot product between the two vectors.

1 Like

thats the problem, i dont want it to detect parts behind.

1 Like

You can use the solution from a similar post

1 Like

ok, i’ve tried that code out, i’ve edited it slightly to be compatible with my code.
its not detecting any part for some reason anymore, im pretty sure its returning false. here’s my new code.

function GetDistanceFromChildren()
if canSwing == true then
local Position_Contain = {}
for Index,Child in pairs(Model:GetChildren()) do
table.insert(Position_Contain,Index,(Child.Position - Core.Position).Magnitude)
end
for Index,Child in pairs(Model:GetChildren()) do
if (Core.Position - Child.Position).Magnitude == math.min(unpack(Position_Contain)) then
print(Child)

  			if Core then
  				return math.acos(Core.CFrame.LookVector:Dot((Child.Position-Core.Position).Unit)) > math.pi/2
  			end
1 Like

Well, thats technically infront of the player, thus the “IsBehindPlayer” returns false.

Facing away from the part means you put the part behind you. And my code does check if it is behind you.

2 Likes

What is Core? Is that the HumanoidRootPart?

Note: My code returns true if it is behind the player. If you want it to return false instead, use this.

return math.acos(Core.CFrame.LookVector:Dot((Child.Position-Core.Position).Unit)) < math.pi/2
1 Like

Core is the HumanoidRootPart, yes.

I’m not much of an advanced scripter, so I dont really understand this, could I have help putting this into my script to make it work please? Or help understanding how this really works. I’m horrible at math to be honest.

also, with this script i dont get any errors but it still doesn’t work.

sorry if im being a pain rn

Ok, let’s debug this. Debugging is the process of finding the error. Change your “IsBehind” code to this

if Core then
	local angle = math.acos(Core.CFrame.LookVector:Dot((Child.Position-Core.Position).Unit))
	print(angle/math.pi*180) -- Turn into degrees
	return angle < math.pi/2
end

Can you tell me what it prints. Then can you send a screenshot of you and the part you are checking.

it prints correctly. it prints the angle im facing

Well, if it prints properly then you should be good with this.

local isInfront = false
if Core then
	local angle = math.acos(Core.CFrame.LookVector:Dot((Child.Position-Core.Position).Unit))
	isInfront = angle < math.pi/2
	if isInfront then
		-- Do something if it's infront
	else
		-- Do something if it's behind
	end
end

-- You can also access it outside the if statement
if isInfront then
	-- Do something if it's infront
else
	-- Do something if it's behind
end
1 Like

that works now, thanks alot, have a good day

1 Like

by any chance would there be a way to make it so that it searches again for the closest object that IS infront of the player? and to make it so that it can still print objects next to the player?

If you aren’t too worried about performance, and have a specific radius in mind, there are new tricks to determine line of sight:

WorldRoot (roblox.com)

GetPartBoundsInRadius: This will get you all parts within X of the player.
GetPartBoundsInBox: If you whitelist the parts from the radius test and look at the box in front of the player, the result is all parts in front of the player within X.

well my pc is kinda a literal potato, so thats not rlly gonna work

If it’s a server script, Roblox uses their computer.

Still it’s the difference between checking one part and all parts

To determine if the football is in front of the red player:

footballCF:ToObjectSpace(redPlayerCF).Z > 0
1 Like

Instead of changing method, would there be a way to alter the script to repeat the loop and check for parts that are infront of the player?

1 Like

okay, ive figured it out.

  		local isInfront = false
  		local isNextTo = false
  		if Core then
  			local angle = math.acos(Core.CFrame.LookVector:Dot((Child.Position-Core.Position).Unit))
  			isInfront = angle < math.pi/2
  			isNextTo = angle < math.pi/1.65
  		end
  		
  		if isInfront or isNextTo then
11 Likes