have a script that spawns NPC’s and uses the FindGameObjectWithTag reference to follow the player, now i need these NPC’s to attack enemys. is there a way to look at another tag for a time while being able to come back to the original?
You can store a reference to the player, and have a different reference for enemies. If there are any enemies around, attack them, but if not (or if the player moves too far), follow the player. You don’t need to get rid of the player reference to do this.
private var player_hero : GameObject;
function Start()
{
player_hero = GameObject.FindWithTag("Player");
}
Also, you don’t want to search for tags the whole time. The only enemies that are relevant to the NPCs are the ones that are within their range. Just add a collider on them and check for tags on collision.
so button makes goul, goul normally follows player, but when enemy is close… go attack enemy. im almost there.
using UnityEngine;
using System.Collections;
public class Goul : MonoBehaviour {
public Transform target;
public Transform player;
public int moveSpeed;
private Transform goul;
public int idleDistance = 1;
public int enemyDistance = 10;
void Awake(){
goul = transform;
}
void Start () {
GameObject to = GameObject.FindGameObjectWithTag(“Enemy”);
target = to.transform;
GameObject go = GameObject.FindGameObjectWithTag(“Player”);
player = go.transform;
void FixedUpdate () {
if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
if (target.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
}
else if (target.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
}
}
if (Vector3.Distance (goul.position, player.position) > idleDistance) {
if (player.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
}
else if (player.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
}
}
so i got them to follow the enemy after hitching a ride with the player, but when the enemy is destroyed it gives me an error.
“object of type ‘Transform’ has been destroyed but you are still trying to access it”
how do i do a check, or how do i ask it if there are any enemies left, and revert back to player as the follow target?
using UnityEngine;
using System.Collections;
public class Goul : MonoBehaviour {
public Transform target;
public Transform player;
public int moveSpeed;
private Transform goul;
public int idleDistance = 1;
public int enemyDistance = 10;
void Awake(){
goul = transform;
}
void Start () {
GameObject to = GameObject.FindGameObjectWithTag("Enemy");
target = to.transform;
GameObject go = GameObject.FindGameObjectWithTag("Player");
player = go.transform;
}
void Update (){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void FixedUpdate () {
if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
if (target.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
}
else if (target.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
}
}
else if (Vector3.Distance (goul.position, player.position) > idleDistance) {
if (player.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
}
else if (player.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
}
}
Debug.DrawLine(player.position, goul.position, Color.yellow);
Debug.DrawLine(target.position, goul.position, Color.green);
}
}
Try checking to see if the target is null. If it is, then don’t try accessing its properties and just go for the player. You should also have some logic checking the player as well, as you will get the same error if your player dies.
You can set up some sort of enemy manager class that keeps track of the number of enemies in the scene, When an enemy dies, notify that class, and you can reference the class with your goul to know how many enemies are left.