Finding angle of an object based of the orientation of another?

I’m trying to make a function that checks whether an object is in a 120 degree cone that originates from an object. Here is a diagram:

Diagram
Note: The 120 degree cone is for the X and Y axes.

What kinds of math would I need to utilize to do this? Thank you!

2 Likes

I wonder: would an absolute value equation work for this?

Example:
x = |3|
https://gyazo.com/e9c76c693c4fc9cacad4381af0e49c95

EDIT: I am not the best with math, however I assume you could combine this absolute value equation with an angle measurement to find a 2D conical shape. If you want to move it to 3D, make sure to multiply by pi.

The steps you’d have to take would be to get the angle of the parts relative to the orientation of the object with the cone, then check if they are within ±half of the angle you want (120/2 = 60)


local function CheckAngle(Player,Object)
	local Distance = (Object.Position - Player.Character.Head.Position);
	local LookVector = (Player.Character.Head.CFrame.lookVector);
	return math.deg(math.acos(Distance:Dot(LookVector) / (Distance.magnitude * LookVector.magnitude)));
end

This is one from my “util” library I use too get the angle between the players face and anthor object, allowing me to check if they are facing said object. You can apply @inlikewithyou logic to actually limit the range of the cone.

6 Likes

That’s a pretty good solution. Can adjust a couple of small things if you want.
The “Distance” name here is a misnomer since that is actually a Vector3 (so don’t let that confuse you). For the same result, you can change:

local Distance = (Object.Position - Player.Character.Head.Position)
...
return math.deg(math.acos(Distance:Dot(LookVector) / (Distance.magnitude * LookVector.magnitude)));

to

local Distance = (Object.Position - Player.Character.Head.Position).Unit
...
math.deg(math.acos( Distance2:Dot(LookVector) ))

Remove all the player references and just use object1 and object2 as args to make the fn more general.

Also, if you don’t need the exact angle value (the value you get with math.deg(math.acos(…))) then you can get away with just using the Distance:Dot(LV) bit and checking the values you get with that. If the lookVector of your object (blue sphere) is looking directly at the other object then the value will be 1. If it is at a 90 deg angle the value will be 0. If the object is behind it will be a negative value. So you can set a condition that says the value returned by Dot needs to be positive and greater than, say, 0.7, and it will eval to true if the blue object is looking more or less at the other. I usually just eyeball that number value to something that looks right, but It’s probably a sine or cosine value that ranges from 0 to 1. If that’s correct, then a 60 deg from center cone (your 120 above) would probably be 0.5 (instead of 0.7).

Anyway, VineyardVine’s suggestion is on track. Let me know if I can be more clear with some of this, I got a bit stream-of-consciousness-y there.

1 Like

Thanks. Although, this was VineyardVine’s solution. I just offered some amendments. Please give him credit for the solution if possible.

1 Like

I wish I could make all the posts solutions, because they all helped me a lot. Thank you!

1 Like

This should be helpful (replace Character/RootPart with your blue object): How to check if an object is within an angle of the front of a player’s characters?

3 Likes