The changes are:
In Unity add a box at the goal area, set its collision box to be a trigger and add a script called GoalAI, here is the code:
using UnityEngine;
using System.Collections;
public class GoalAI : MonoBehaviour {
public PlayerController playerControl;
public ManagerAI manager;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
manager.playerWon();
playerControl.playerWon();
}
}
}
Now update the playerController script, here is the script updated:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour {
public ManagerAI manager;
private int health;
private bool playerWonFlag;
public List<GameObject> bullets;
// Use this for initialization
void Start () {
health =3;
playerWonFlag = false;
}
private void shoot (Vector3 startPoint, Vector3 target)
{
foreach(GameObject bullet in bullets)
{
if(bullet.GetComponent<bulletAI>().isFree())
{
bullet.GetComponent<bulletAI>().shoot(startPoint, target);
break;
}
}
}
// Update is called once per frame
void Update () {
if(health <1 || playerWonFlag == true)
{
return;
}
float speed = 10 * Time.deltaTime;
Vector3 pos = this.GetComponent<Transform>().position;
Vector3 mousePoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f ));
mousePoint.y = pos.y;
if(Input.GetKeyDown(KeyCode.Mouse0))
{
shoot(pos, mousePoint);
}
if(Input.GetKey(KeyCode.UpArrow))
{
pos.z += speed;
}
if(Input.GetKey(KeyCode.DownArrow))
{
pos.z -=speed;
}
if(Input.GetKey(KeyCode.RightArrow))
{
pos.x += speed;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
pos.x -= speed;
}
this.GetComponent<Transform>().position = pos;
this.GetComponent<Transform>().LookAt(mousePoint);
}
public void playerHit(Vector3 hitPoint)
{
Vector3 direction = this.GetComponent<Transform>().position - hitPoint;
this.GetComponent<Rigidbody>().velocity = direction.normalized * 20;
AudioSource source = this.GetComponent<AudioSource>();
source.Play();
health = health-1;
if(health <1)
{
die();
}
//Debug.Log ("Hit");
}
private void die ()
{
this.GetComponent<Transform>().Rotate(new Vector3 (90.0f, 0, 0));
manager.playerLost();
}
public void playerWon()
{
playerWonFlag = true;
}
}
Now update the ManagerAI script, here is the updated code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ManagerAI : MonoBehaviour {
private float GENERATE_TIME_INTERVAL = 2;
private int GAMESTATE_PLAYING = 0;
private int GAMESTATE_DEAD = 1;
private int GAMESTATE_WIN = 2;
public Text UIText;
public List<GameObject> enemies;
public List<GameObject> generators;
private int gameState;
public GameObject player;
private float curTime;
// Use this for initialization
void Start ()
{
UIText.text = "";
gameState = GAMESTATE_PLAYING;
curTime = 0;
}
// Update is called once per frame
void Update ()
{
if (gameState == GAMESTATE_PLAYING)
{
curTime += Time.deltaTime;
if (curTime > GENERATE_TIME_INTERVAL && enemyIsAvailable())
{
//Debug.Log("Update Manager AI");
curTime = 0;
generateEnemyCloseToPlayer();
}
}
}
private void idleEnemies()
{
foreach(GameObject enemy in enemies)
{
enemy.GetComponent<EnemyAI>().stopHunting();
}
}
public void playerWon()
{
gameState = GAMESTATE_WIN;
idleEnemies();
UIText.text = "YOU WIN!";
}
public void playerLost()
{
gameState = GAMESTATE_DEAD;
idleEnemies();
UIText.text = "GAME OVER";
}
private bool enemyIsAvailable()
{
foreach (GameObject enemy in enemies)
{
if (enemy.GetComponent<EnemyAI>().isEnemyIdle())
{
return true;
}
}
return false;
}
private void generateEnemyCloseToPlayer()
{
Vector3 genPos = getGeneratorCloserToPlayer().GetComponent<Transform>().position;
genPos.y += 0.3f;
GameObject enemy = null;
foreach (GameObject curEnemy in enemies)
{
//Debug.Log("generateENemy");
if (curEnemy.GetComponent<EnemyAI>().isEnemyIdle())
{
curEnemy.GetComponent<Transform>().position = genPos;
curEnemy.GetComponent<EnemyAI>().startHunting();
break;
}
}
}
private GameObject getGeneratorCloserToPlayer()
{
GameObject closerGenerator = null;
foreach(GameObject generator in generators)
{
//Debug.Log("getGeneratorCloser");
if (closerGenerator == null)
{
closerGenerator = generator;
}
else
{
Vector3 posOne = closerGenerator.GetComponent<Transform>().position;
Vector3 posTwo = generator.GetComponent<Transform>().position;
Vector3 playerPos = player.GetComponent<Transform>().position;
float distOne = Vector3.Magnitude(playerPos - posOne);
float distTwo = Vector3.Magnitude(playerPos - posTwo);
if (distTwo < distOne)
{
closerGenerator = generator;
}
}
}
return closerGenerator;
}
}
Update the EnemyAI script, here is the updated script:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
private int STATE_IDLE=0;
private int STATE_HUNTING=1;
private int STATE_DEAD=2;
public int aiState;
public GameObject m_player;
private float waitTime;
private Vector3 idlePos;
// Use this for initialization
void Start ()
{
idlePos = this.GetComponent <Transform>().position;
aiState = STATE_IDLE;
}
public bool isEnemyIdle()
{
return (aiState == STATE_IDLE);
}
public void startHunting()
{
aiState = STATE_HUNTING;
}
// Update is called once per frame
void Update ()
{
if(aiState == STATE_HUNTING)
{
if(waitTime >= 0)
{
waitTime -= Time.deltaTime;
//Debug.Log ("time " + waitTime);
}
else
{
Vector3 playerPos = m_player.GetComponent<Transform>().position;
Vector3 pos = this.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = Vector3.MoveTowards(pos, playerPos, 0.07f);
}
}
else if(aiState == STATE_DEAD)
{
if(waitTime >= 0)
{
waitTime -= Time.deltaTime;
}
else
{
reset ();
}
}
else
{
//Debug.Log ("Idle");
// DO NOTHING
}
}
private void reset()
{
aiState = STATE_IDLE;
this.GetComponent<Transform>().position = idlePos;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
this.GetComponent<Transform>().rotation = Quaternion.Euler(new Vector3(0,0,0));
aiState = STATE_IDLE;
}
void OnTriggerEnter(Collider other)
{
if(aiState == STATE_DEAD )
{
return;
}
if(other.tag == "Player")
{
waitTime = 0.5f;
m_player.GetComponent<PlayerController>().playerHit(this.GetComponent<Transform>().position);
}
else if (other.tag == "bullet")
{
die (m_player.GetComponent<Transform>().position);
}
}
public void stopHunting()
{
aiState = STATE_IDLE;
}
private void die(Vector3 playerPos)
{
waitTime = 2f;
aiState = STATE_DEAD;
Vector3 pos = this.GetComponent<Transform>().position;
this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
this.GetComponent<Rigidbody>().AddForce(( pos - playerPos).normalized*600);
}
}
In Unity drag and drop the manager to the player updated script variable (called manager)
Do the same for player and manager into the goal object.
Add a text UI object and place it in the middle of the screen (0,0,0) and drag and drop it into the manager updated script.
Try to run it.