- Home /
how to make enemy clone work independently?
so I am making a shooting game where the game spawns 5 enemies from a prefab. the problem is that when the enemy clone gets instantiated it works properly, but when the enemy clone detects the player, all of the enemy clones start shooting at the player.
I want the enemy clone that detected the player to be the only one that will shoot at the player, and not all of the enemy clones.
here is the code for my enemy object:
using UnityEngine;
using System.Collections;
public class enemyMovement : MonoBehaviour
{
Rigidbody2D rb2d;
public Transform target;
private float nextFire;
public float turnTime;
public float speed;
public float trackSpeed;
int turn;
public GameObject particles;
int enemyHp = 100;
private float explodeTime;
// Use this for initialization
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
particles.gameObject.SetActive(false);
target = GameObject.Find("fighter").transform;
}
float h, v, zed;
public static bool check = true;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (check == true)
{
if (Time.time > nextFire)
{
nextFire = Time.time + turnTime;
turn = Random.Range(0, 5);
switch (turn)
{
case 1:
h = horizontal - 1;
v = vertical;
rb2d.transform.eulerAngles = new Vector3(0, 0, 90);
// Debug.Log("left");
break;
case 2:
h = horizontal + 1;
v = vertical;
rb2d.transform.eulerAngles = new Vector3(0, 0, 270);
// Debug.Log("right");
break;
case 3:
h = horizontal;
v = vertical + 1;
rb2d.transform.eulerAngles = new Vector3(0, 0, 0);
// Debug.Log("up");
break;
case 4:
h = horizontal;
v = vertical - 1;
rb2d.transform.eulerAngles = new Vector3(0, 0, 180);
// Debug.Log("down");
break;
}
}
Vector2 temp = new Vector2(h, v);
rb2d.AddForce(temp * Time.deltaTime * speed);
}
else if (check == false)
{
Vector3 vectorToTarget = target.position - transform.position;
float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
if ((target.position.x > rb2d.position.x + 7 && target.position.x < rb2d.position.x + 15)
|| (target.position.x < rb2d.position.x - 7 && target.position.x > rb2d.position.x - 15)
|| (target.position.y > rb2d.position.y + 5 && target.position.y < rb2d.position.y + 15)
|| (target.position.y < rb2d.position.y - 5 && target.position.y > rb2d.position.y - 15))
{
transform.position += transform.up * Time.deltaTime * trackSpeed;
}//if the enemy is within the range of the player, the enemy will follow the player.
else if(target.position.x > rb2d.position.x + 15 || target.position.x < rb2d.position.x - 15 || target.position.y > rb2d.position.y + 15 || target.position.y < rb2d.position.y - 15)
{
check = true;
}//if the enemy is outside the range of the player, the enemy will resume its random movement.
}
if(enemyHp == 0)
{
Destroy(gameObject);
enemySpawn.maxEnemy--;
particles.gameObject.SetActive(true);
if (Time.time > explodeTime)
{
explodeTime = Time.time + .5f;
particles.gameObject.SetActive(false);
}
}
// Debug.Log(check);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("player fighter"))
{
check = false;
}// if the player touches the onTrigger for the enemy, the enemy will follow the player.
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("player bullet"))
{
Destroy(other.gameObject);
enemyHp -= bulletMove.bulletDamage;
}
}
}
Answer by eldruz · Feb 28, 2017 at 02:18 PM
It seems the boolean you use for player detection, check
, is a static variable. As such, it is shared by all instances of your class, meaning that once an enemy sees the player, they all do.
You should just use a non static variable for player detection.
Your answer
Follow this Question
Related Questions
How do i Instantiate gameObjects in between multiple points? 2 Answers
How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers
Instantiating at different rates 1 Answer
Instantiated object not showing in scene or hierarchy 2 Answers
How do i Instantiate sub-Points with in Multiple Points?? 1 Answer