How to check if a part is in a given direction

Hello. I am messing about with global wind and I need a way to tell if a part is in an area in the direction of the wind. Example:

Red = The direction of the wind
Green = The part we are checking from
Blue = The part we are checking for
Purple = The area that blue is being checked

Any thoughts would be greatly appreciated. Many thanks

1 Like

This will check whether the center of the blue part is in the area.

local observer: Part = -- the green part
local subject: Part = -- the blue part

local vectorStartPointLocalSpaceOffsetFromObserver: Vector3 = .5 * observer.Size.Z * -Vector3.zAxis -- This is a guess based on the picture but you can change this.
local maxAngleDifference: number = math.rad(45)

local function isSubjectCenterInArea(): boolean
	local vectorStartPoint: Vector3 = observer.CFrame * vectorStartPointLocalSpaceOffsetFromObserver

	local vectorToSubject: Vector3 = subject.Position - vectorStartPoint
	local horizontalVectorToSubject = vectorToSubject - vectorToSubject.Y * Vector3.yAxis
	local horizontalGlobalWind: Vector3 = workspace.GlobalWind - workspace.GlobalWind.Y * Vector3.yAxis
	local angleDifference: number = horizontalVectorToSubject:Angle(horizontalGlobalWind)

	return angleDifference <= maxAngleDifference
end

Edit: I had accidentally written math.deg instead of math.rad.

2 Likes

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