-1
\$\begingroup\$

I am trying to make a game using NavMesh agent in Unity, where the enemies first attack the player and change their target to tanks when player spawns them. Then again retarget player after the tank dies, have no idea how to do this as tank is not in game initially and cannot check it in awake.

using UnityEngine;
using System.Collections;

public class enmymove : MonoBehaviour {

Transform player;
NavMeshAgent nav;
healthscript Health;
Animator anim;
public GameObject GORTLE;
Transform Gortle;
Gortlehealth Ghealth;


void Awake ()
{
    player=GameObject.FindGameObjectWithTag ("Player").transform;
    nav = GetComponent<NavMeshAgent>();
    Health =player.GetComponent<healthscript> ();
    anim = GetComponent<Animator>();
    //Gortle = GameObject.FindGameObjectWithTag ("Gortle").transform;
    //Ghealth = Gortle.GetComponent<Gortlehealth> ();
}


void Update ()
{

    if (Ghealth.currenthealth > 0 ) 
    {

        nav.SetDestination (Gortle.position);

    }
    else 
    {
        if (Health.currenthealth > 0 && Ghealth.currenthealth <= 0 ) 
        {
            nav.SetDestination (player.position);
        }
        else
        {
            GetComponent<NavMeshAgent> ().enabled = false;
            anim.SetTrigger ("idle");
        }
    }
 }
 }
\$\endgroup\$
0
\$\begingroup\$

Easiest approach would be to have a list of spawned Tanks in Player object ( and have some callback for Tank to call, when it dies, or check if it's alive in Player's Update() to remove dead Tanks from list ).

And then in your Enemy class in Update() just look at this list. If it's empty or all listed tanks are dead ( eg. list was not updated yet ) - attack Player. If it has any living Tanks - attack them.

You have requested for some code, so here's a stub:

using UnityEngine;
using System.Collections.Generic;

public class Tank : MonoBehaviour {} // remember to not Destroy it on kill, just SetActive(false)

public class Player : MonoBehaviour {
    public List<Tank> tanks = new List<Tank> ();
    public Tank prefab;  // set this to your Tank prefab in Editor
    private bool click = false;

    void Update () {
        if (Input.GetMouseButtonDown(0) && !click && prefab != null) {
            click = true;
            Tank r = Instantiate (prefab, transform.position, Quaternion.identity) as Tank;
            // ^ this will spawn one tank in Player position
            tanks.Add (r);
        }
        if (Input.GetMouseButtonUp (0) && click) {
            click = false;
        }
        // reverse for, not foreach, because we modify list
        for (int i = tanks.Count - 1; i >= 0; i--) {
            if (tanks[i] == null) {
                tanks.RemoveAt(i);
            }
            if (!tanks[i].isActiveAndEnabled) {
                Destroy (tanks[i].gameObject);
                tanks.RemoveAt(i);
            }
        }
    }
}

public class Enemy : MonoBehaviour {

    void Update () {
        Player p = GameObject.FindWithTag ("Player").GetComponent<Player> ();
        if (p != null) {
            if (p.tanks.Count > 0) {
                Tank t = p.tanks [0]; // or choose some other Tank
                shootToTank (t); // implement this in your own way, just remember to not Destroy it on kill, just SetActive(false)
            } else {
                shootToPlayer (p);  // implement this in your own way
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ any example i'm totally new to scripting \$\endgroup\$ – sparkblack Jan 9 '16 at 21:32
  • \$\begingroup\$ @sparkblack added a simple stub \$\endgroup\$ – Marqin Jan 9 '16 at 23:24
  • \$\begingroup\$ And how's it working for you? \$\endgroup\$ – Marqin Jan 11 '16 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.