Create Spherical Trigger and Cone Trigger.

Suleiman Abdullah
6 min readApr 14, 2024

Objective: Create Spherical Trigger and Cone Trigger.

Before doing this ,you need to know we will start this from the base of Wedge trigger ,so we will use some variable from there ,so check a Wedge Trigger article plus another article how to add Inner Trigger in Wedge Trigger.

In the first step lets create an enum called Shape in the WedgeTrigger Class and define three variable inside called Wedge ,Spherical and Cone. Then we need to create a variable of Shape named as shape starting with small s letter.

Go to OnDrawGizmos cut this code for drawing Wedge Trigger, then paste in new method called DrawWedgeGizmo , I didn't tell you how I create this method so create it now.

Create another method called DrawSphericalGizmo and DrawConeGizmo.

Go to OnDrawGizmos ,create a switch statement which will check shape and call each method respectively according to its enum.

Those above Gizmo matrix we talk about it from previous article so if you are new read info when beginning this article.

Create a return type method called WedgeContains,go to Contains method cut all code for Wedge trigger and paste them to Wedge Contains.

Create two return method called Spherical Contains and Cone Contains.

Drawing Sphere Gizmos

To Draw Spherical We will use Gizmos dot DrawWireSphere passing Vector3 dot zero and inner Radius ,I will repeat this but this time we pass Outer Radius.

Drawing Cone Gizmos

To Draw Cone Gizmo we will need to copy this code from DrawWedgeGizmo. We did this because we will need to reuse vVectors whether its a right one or left.

Note: We don't need a top one.

We need to draw an arc so we will copy an arc code from wedge which draw below arch from innerradius and outerRadius.

To make our cone gizmo draw in white we need to set ConeContains return true to see clear.

Now we need two line to draw from vleft Outer to vLeftInner and vRightOuter to vRightInner.Good new we have this code from Wedge so we need to borrow them to our DrawconeGizmos.

If you are new in code please type these line ,I copy this code for the sake of this article

Go inside the DrawConeGizmo, create local method called DrawFlatWedge and paste the code of drawing part of the cone into DrawFlatWedge, then call that method(DrawFlatWedge) inside DrawConeGizmo.

Create a method called SetGizmoMatrix which take 4by4 matrix as parameter and set handles and Gizmos matrix as this parameter.

Now we need to draw a FlatWedge but In Vertical we can do this using Matrix, to do this go to DrawConeGizmo then store the current matrix of Gizmo then Multiply matrix with certain rotation. Last Call DrawFlatWedge then Reset the gizmo matrix to initial matrix.

I have post some matrix notes on my previous post, go learn about them but we can use matrix to multiply by it self to rotate object ,vectors.

Now we need to draw circle ,we need to know a distance from the outer cone to the center ,we can use Trigonometry formula to do this,if you look at our cone need to be like this ,to get d = cos(alpha/2),but since our distance is grow direct proportional to the radius we will multiply by radius d =rdius * cos(alpha/2).And to get the y = radius * sin(alpha).Our field of view need to be half of it because we work on the upper tringle only that's why we divide by 2.

Note: We need to define out center which will use vector3 and Z as d.

To Draw circle we use handle.DrawWireDisc ,then we need to put all this code in local method called DrawRing ,then we call this method twice by passing InneRadius and outerRadius,in the DrawConeGizmo.

Note: You may ask your self where is fieldofviewRadian(fovRad) came from, we define it when draw Wedge.

fovRad we define it early ,just add this Image to remind you

Trigger Calculation for SphericalContains

We need to check distance between turret and a target and use that distance to check if distance is less or equal to outer radius or distance is greater or equal inner radius ,this means we are inside else we will be outside of this.

Trigger Calculation for ConeContains

We will use our sphere contain to check if we are inside within inner Radius or outerRadius ,then we will use angle to check if we are inside the angle of the cone .

Here we use Acos to find angle ,but this method take two normalize Vector so I will use tranform.forward and direction to Target ,but I will clamp a dot product due to floating point precision Issues.

Note: You can clamp whether between Zero and 1 or between -1 and 1 ,I clamp between -1 and 1 cos I will update my angle to go more than 180 degree.

Go to Contains method ,i will this equal and arrow method which means return true if contain has target position ,then we enter in switch statement which check shape with it state the it return the calculation for that shape.

Note: If you are use old Unity version and Old visual studio this C# feature wont work ,I tried this on unity 2017 and visual studio 2017 ,I got error now am using Visual studio Community 2019 and unity 2021.3.36f1

This method is called in Turret method, If you forget our previous implementation of turret behavior.

This how I Create Spherical Trigger and Cone Trigger with its Visual.

See you in the next one

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development