Tutorial: a turret and bullets

A turret that turns to face the player and shoots every second or so, only when nothing is in the way. Then a better bullet as a reusable prefab.

1. The turret

The turret

Put the script on the turret's head (a sphere or cube sitting on a base).

Scripts/Turret.cs

using System.Collections;

public class Turret : Behaviour
{
    public float range = 20f;
    public float secondsBetweenShots = 1.2f;
    public float shotSpeed = 18f;

    GameObject player;
    float wait;

    public override void Start() { player = GameObject.FindPlayer(); }

    public override void Update(float deltaTime)
    {
        if (player == null) return;
        Vector3 aim = player.transform.position + Vector3.up;
        if (Vector3.Distance(transform.position, aim) > range) return;
        transform.LookAt(aim);
        wait -= deltaTime;
        // a wall between the two stops the shot (the player themselves does not)
        bool blocked = Physics.Linecast(transform.position, aim, out RaycastHit hit, Physics.DefaultRaycastLayers, gameObject) && hit.gameObject != player;
        if (wait <= 0f && !blocked)
        {
            wait = secondsBetweenShots;
            StartCoroutine(Shoot());
        }
    }

    IEnumerator Shoot()
    {
        GameObject ball = GameObject.Spawn("Sphere", transform.position + transform.forward, Vector3.zero, new Vector3(0.3f, 0.3f, 0.3f));
        ball.SetColor(Color.red);
        ball.AddRigidbody(0.5f);
        ball.rigidbody.velocity = transform.forward * shotSpeed;
        Audio.PlayAt("Audio/shot.wav", transform.position);
        yield return new WaitForSeconds(4f);
        if (ball.exists) GameObject.Destroy(ball);
    }
}

Save it as Scripts/Turret.cs.

How it works: transform.LookAt turns the head; Physics.Linecast checks that no wall blocks the shot; StartCoroutine(Shoot()) starts a routine that makes a ball, throws it with velocity, and destroys it after four seconds (yield return new WaitForSeconds). Tune range, secondsBetweenShots and shotSpeed in the Inspector.

2. A better bullet

A better bullet

Balls from a rigid body can pass through a thin wall if they are fast. This bullet checks the path it travelled each frame:

Scripts/Bullet.cs

public class Bullet : Behaviour
{
    public float speed = 60f;
    public float damage = 25f;
    public float lifeSeconds = 3f;

    float age;

    public override void Update(float deltaTime)
    {
        Vector3 from = transform.position;
        Vector3 to = from + transform.forward * speed * deltaTime;
        // the whole step of this frame is checked, not only where the bullet ends up: at 60 m/s a frame's step is a metre
        if (Physics.Linecast(from, to, out RaycastHit hit, Physics.DefaultRaycastLayers, gameObject))
        {
            hit.gameObject.SendMessage("TakeDamage", damage);
            if (hit.gameObject.rigidbody.exists) hit.gameObject.rigidbody.AddImpulse(transform.forward * 2f);
            GameObject.Destroy(gameObject);
            return;
        }
        transform.position = to;
        age += deltaTime;
        if (age > lifeSeconds) GameObject.Destroy(gameObject);
    }
}

Put it on a small sphere and save the sphere as a prefab by dragging it into the Project panel. A gun makes one with:

GameObject shot = GameObject.Instantiate("Prefabs/Bullet.prefab", muzzle);
shot.transform.rotation = transform.rotation;

The bullet hurts the first thing in its way with the same TakeDamage message as in health, so a turret, a barrel and a guard all react.

Next: an exploding barrel.