How to make the enemy kill a specific player instance?,How do I kill a specific Player instance?
In my game the player controls a swarm instead of just one specific player, I am trying to make it so when the enemy attacks one of the players it kills the specific one it collides with, however right now it just kills whichever one the camera is focused on, or the "main" one (although there is no difference between it and the others besides the camera following it)
public class BasicEnemyAttack : MonoBehaviour
{
public float cooldown;
public float attackStart;
public Transform attackPosition;
public float range;
public LayerMask whatIsEnemy;
public int damage;
public Animator anim;
public Transform target;
float distance;
void Start()
{
cooldown = 5;
distance = Vector2.Distance(gameObject.transform.position, target.transform.position);
}
void Update()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
float distance = Vector2.Distance(gameObject.transform.position, target.transform.position);
if (cooldown > 0)
{
cooldown -= Time.deltaTime;
}
if (cooldown <= 0)
{
cooldown = 5;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (cooldown <=2.5 && collision.transform.gameObject.tag == "Player" && GetComponent<Enemy>().health > 0)
{
anim.SetTrigger("Attacking");
target.GetComponent<MushyHealth>().TakeDamage(damage);
Debug.Log("Bitten");
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPosition.position, range);
}
}
, Basically, in my game the player controls a swarm of "Players" what I am trying to accomplish right now is to have when the enemy attacks one of the players it kills the one it specifically collided with. However, right now when it collides with one of the players it kills the one that has the camera focused on it.
void Start()
{
cooldown = 5;
distance = Vector2.Distance(gameObject.transform.position, target.transform.position);
}
void Update()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
float distance = Vector2.Distance(gameObject.transform.position, target.transform.position);
if (cooldown > 0)
{
cooldown -= Time.deltaTime;
}
if (cooldown <= 0)
{
cooldown = 5;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (cooldown <=2.5 && collision.transform.gameObject.tag == "Player" && GetComponent<Enemy>().health > 0)
{
anim.SetTrigger("Attacking");
target.GetComponent<MushyHealth>().TakeDamage(damage);
Debug.Log("Bitten");
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPosition.position, range);
}
}
Sorry for the formatting, first time posting here and it got a little screwy :(