Skip to content

Instantly share code, notes, and snippets.

@tinyhappysteps
Created February 15, 2016 14:24
Show Gist options
  • Save tinyhappysteps/bd4b48ed95bdd7f57055 to your computer and use it in GitHub Desktop.
Save tinyhappysteps/bd4b48ed95bdd7f57055 to your computer and use it in GitHub Desktop.
GameMaster.cs and WhiteAnt.cs - basic AI working
// GameMaster.cs ***
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public GameObject whiteAnt;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Instantiate(whiteAnt, new Vector3(Random.Range(0,12f),Random.Range(0f,16f),0),Quaternion.identity);
Instantiate(whiteAnt, new Vector3(Random.Range(0,12f),Random.Range(0f,16f),0),Quaternion.identity);
Instantiate(whiteAnt, new Vector3(Random.Range(0,12f),Random.Range(0f,16f),0),Quaternion.identity);
}
}
}
// WhiteAnt.cs ***
using UnityEngine;
using System.Collections;
public class WhiteAnt : MonoBehaviour {
void Start () {
audio.Play();
}
// Update is called once per frame
void Update () {
//this.transform.position = Vector3.MoveTowards(this.transform.position, this.transform.position + new Vector3(Random.Range(-1f,1f),Random.Range(-1f,1f),0), 0.1f);
var Ants = GameObject.FindGameObjectsWithTag("Ant");
Transform tMin = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = this.transform.position;
foreach(var x in Ants)
{
float dist = Vector3.Distance(x.transform.position, currentPos);
if (dist < minDist && dist > 0f)
{
tMin = x.transform;
minDist = dist;
}
}
Debug.Log (minDist);
this.transform.position = Vector3.MoveTowards(this.transform.position, tMin.position, 0.05f);
}
}
// SOURCE PROJECT: https://www.dropbox.com/s/55hnq7cbgiwe43l/Ant%20Battle%20First%20AI%20working.zip?dl=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment