Hello everybody.
I’m making a 2D game that likes a tower defence. I’m writing the enemy’s AI scripts that will go to the planet and start to attack it. But I can’t make the enemies take out the planet’s life, and I don’t know why…
I’m new in this, can you help me?
Script called “EnemyAI” it’s the enemies’ AI, the “PlanetLife” it’s the planet’s life.
Enemies AI’s code:
using UnityEngine;
using System.Collections;
using DG.Tweening;
public class EnemyAI : MonoBehaviour {
public Transform target;
NavMeshAgent spaceship;
public Vector3 distTarget;
private PlanetLife life;
public enum States
{
Invading,
Hitting,
Attacking
}
public States state = States.Invading;
void Awake()
{
life = GetComponent<PlanetLife> ();
}
void Start ()
{
spaceship = GetComponent<NavMeshAgent> ();
}
void Update () {
switch(state)
{
case States.Invading:
StateInvading();
break;
case States.Attacking:
StateAttacking();
break;
case States.Hitting:
StateHitting();
break;
default :
Debug.LogError("BUG: Cannot stay without state.");
break;
}
}
private void StateInvading()
{
distTarget = target.position - transform.position;
spaceship.SetDestination (target.position);
if (distTarget.magnitude < 15)
{
state = States.Hitting;
}
}
private void StateHitting()
{
spaceship.Stop ();
life.LIFE -= 10;
Debug.Log (life.LIFE + " remaining lives.");
}
private void StateAttacking()
{
}
}
Planet’s life code
using UnityEngine;
using System.Collections;
public class PlanetLife : MonoBehaviour
{
public float LIFE;
public void Start ()
{
LIFE = 1000;
}
}