O'Reilly logo

Unity 4.x Game AI Programming by Thet Naing Swe, Aung Sithu Kyaw, Clifford Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Weapons

Our player-controlled car has two weapons; a missile launcher and a normal gun, while the AI cars only have a normal gun. Let us look at them to see how they're implemented. There are not many AI techniques here though.

Gun

The WeaponGun class simply spawns bullets upon calling its Shoot method.

using UnityEngine;
using System.Collections;

public class WeaponGun : MonoBehaviour {
  public GameObject Bullet;
  public GameObject[] GunGraphics;
  public float ratePerSecond;
  private bool bShoot;

  // Use this for initialization
  void Start() {
    bShoot = false;
  }

  public void Shoot() {
    bShoot = true;

    foreach (GameObject obj in GunGraphics) {
      obj.animation.CrossFade("GunShooting", 0.5f);
    }

    StartCoroutine("ShootBullets");
  }

When shooting bullets we don't want ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required