Menu
CoddyTech

Recap: Turret

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 46 of 71.

This recap puts the chapter together: a direction flattened to the ground, Quaternion.LookRotation, a steady turn with RotateTowards, and an offset turned by a rotation.

challenge icon

Challenge

Medium

Write the turret: it tracks the tank and fires shells.

  1. Each frame, take tank.position - transform.position with its y set to 0, and turn toward Quaternion.LookRotation of it with Quaternion.RotateTowards, at turnSpeed * Time.deltaTime.
  2. On Space, spawn shellPrefab at transform.position + transform.rotation * new Vector3(0, 0.5f, 1), with transform.rotation.

Press Play, drive the tank and fire. Then Check.

Try it yourself

using UnityEngine;

public class Turret : MonoBehaviour
{
    public Transform tank;
    public GameObject shellPrefab;
    public float turnSpeed = 90f;

    void Update()
    {
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay

Practice on your own: Unity playground