Interested in this Exercise?

Watch the course trailer to see what's inside, read the description, and then start learning on CG Cookie today.

Watch course trailer Start Learning

Creating a Fixed Turret

  • Software:Unity 2018  ·
  • Difficulty:Beginner

Rules of the Exercise

  • 1
    Allow rotation of turret along base and aiming of the barrels up and down with a clamped range of 40 degrees total
  • 2
    Rotate barrels quickly only while holding down left click
  • 3
    Apply force to nearby Rigidbody objects using a Raycast

Creating Stationary Weapons

Often times in shooters you'll come across a heavy weapon that is fixed in a certain location with limited range of motion to fend off incoming hordes of enemy characters or even on moving vehicles to defend them. In this exercise you'll start off with a character that is fixed to a weapon using IK and will move side to side to mimic the effect of the player aiming the weapon in a certain direction. The goal of this exercise is to clamp down the range of this weapon in a few different directions so that the player looks like they can control the weapon and not the other way around. You'll take it a step further and use this as a minigun with spinning barrels. while holding the trigger down (mouse click) the barrels will spin and you'll also apply force to nearby rigidbody objects. Download the starting project files to get started with this exercise.


Utilize the project files included, you are free to add additional features like audio and a muzzle flash but the turret and player must stay the same. Submit your exercise when ready to be graded, have fun!

How To

If you have not completed the exercise on your own do not proceed. This is the how to section for this exercise to be used to determine how this was accomplished. If you're feeling stuck or need to know how this was done then take a look below.

The first rule was to rotate the base and the barrels separately using the mouse input with a clamped range of 40 degrees. Below is a code snippet of the base/barrels methods:

    void BaseRotation ()
        moveX += Input.GetAxis("Mouse X") * speed;
        moveX = Mathf.Clamp(moveX,-20,20); 
        baseRotation = Quaternion.Euler(0,moveX,0);
        turretBase.rotation = Quaternion.Lerp(turretBase.rotation, baseRotation,1f);
    void BarrelRotation ()
        moveY -= Input.GetAxis("Mouse Y") * speed;
        moveY = Mathf.Clamp(moveY,-20,20); 
        barrelRotation = Quaternion.Euler(0,moveX,moveY);
        turretBarrels.rotation = Quaternion.Lerp(turretBarrels.rotation,barrelRotation,1f);

By using Mathf.Clamp we can designate a range of motion for the base and the barrels. Both methods are nearly identical but using different inputs and axes to rotate around. 

The second rule was to rotate the barrels while holding down left click. This is done by referencing the x axis of the barrels then using transform.Rotate. See the code snippet down below:

public Transform barrels;
barrels.Rotate(Vector3.right, 20f);

Lastly we also wanted to apply a force to objects within line of sight of the turret barrels while holding down left click to mimic the effect of a minigun firing at these objects. We used a Raycast for this, take a look down below to see how this was done:

void Detection ()
        Debug.DrawRay(barrelTip.position, barrelTip.forward * rayDistance,;
        RaycastHit hit;
        if(Physics.Raycast(barrelTip.position, barrelTip.forward, out hit, rayDistance, rayMask))
            if(hit.rigidbody != null)
                hit.rigidbody.AddForceAtPosition(barrelTip.forward * appliedForce, hit.point);

We check to see if the object we hit has a rigidbody, if it does then we can apply a force at the impact point of where the raycast hit. Any additional mechanics added in the exercise are optional. Take a look at the completed project files in the downloads tab for full code and assets.