How to create Wedge Trigger for a Turret.

Suleiman Abdullah
6 min readMar 26, 2024

Objective: How to create Wedge Trigger for a Turret.

Create three variable and name them as following

We need to understand how this will come, we have turret forward position and directionToTarget ,our dot product here is angleThresh (This will be our angle but it is not actual angle)

The top doted line is x not q

Now we need to find x ,we do this using pythagoras theory. But we need to know our target to direction need to be normalized, so we now its value is one.

Widget Trigger Visual

This part is to visualize so its not a calculation ,this below code convert our gizmos from local to world. Then we define the top by height.

This code draw sphere bottom and top with radius, default is a Vector3.Zero which a center in a world coordinate system.

We define two line a left and right which will form widget .

We draw two line from the bottom sphere and two line on top sphere.

We connect line from top left line to bottom left line and top right line to bottom right line and center to top.

Take Widget Trigger and put inside the turret model ,reset widget position .

Real Calculation

Before start calculation I disable the turret to focus on this feature.

Create a return method which return a bool called Contains with a vector3 parameter.

Now we need to find a direction vector which will be in world position.

Now we need to convert this vector from world space to local space ,we do this to avoid complext step when this vector is in world space. We convert this vector from world to local space using transform.InverseTransformVector we pass a vectorToTarget as its parameter.

Note: In other word to make things simple you need to make sure direction to the target is local space.

Now we need to make direction to target ,we create a Vector3 variable and assign our vectorToTargetLocal and normalize it.

Now we need to check if the target is within the angle of this cylindrical sphere .We do this using direction To Target z-axis if is less than angleThresh.

If we use this direction to check our angle of widget we will have an issue where if our target is above the y of directionToTargetVector it wont detect if we are inside. Remember here we are checking an infinity angle .

Note: I have change color of gizmos to white if the above condition is true and red if condition is false, here is code.

Solution we need to use flatDirectionToTarget ,so we will change the name of direction to target to flatDirectionToTarget ,then we will flat y axis of the direction to target then normalize it. This flatDirection will be used to change the angle.

To check the height we need to use a y-axis of vectorToTarget by comparing if its greater than a height for upward check and comparing its y-axis with zero to check if the target is below the cylindrical.

In this step we need to check how far away is target along the flatDistance, to find flat Distance we will use flatDirectionToToTarget.

This is how to create cylindrical with angle check for a turret.

Bonus

We want a turret to point at target, so create three variable a transform variable for target, Wedge Trigger variable called trigger, and transform variable for Gun. Then drag and drop those object in the inspector.

I will check if a Widget contain a target inside OnDrawGizmos.

To point at target we will need to calculate a vectorToTarget ,but this time we won’t convert it to local space ,since we will use Quaternion.LookRotation which is take world space direction and up vector.

To make rotate smoothly we will need to ,we need to create a Quaternion handle and assign it to our Quaternion.LookRotation,then using Slerp to rotate from gunTransform rotation to targetRotation .I have set smoothing factor to 4 in the inspector.

Note: This code need to run in update since we need to use Time.deltaTime to make Slerp be frame rate Independent.

Last we will need our turret to rotate in the final position when our target go out of the range. We do this by create a global variable of targetRotation and replace it with local one ,then we need to move out the Slerp from If condition.

This is it.

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