How to avoid my player being killed twice
Hi Everyone,
I've been trying to get my head around something that's happening in my game. It's a kind of platformer. And the thing is that a specifical type of enemy attack is killing my player twice.
This is the script that takes damage:
void Update()
{
if (immune)
{
if (immuneTimer > 0)
{
immuneTimer -= Time.deltaTime;
}else{
immune = false;
immuneTimer = 2f;
}
}
}
void Start()
{
rend = GetComponent<Renderer>();
currentHP = hp;
//immuneTimer = immunityTime;
}
void OnTriggerEnter2D (Collider2D col)
{
if (!immune)
{
DamageSource damageGiver = col.GetComponent<DamageSource> ();
if (damageGiver) {
if (this.tag == "Enemy")
{
SendMessageUpwards ("Defend", SendMessageOptions.DontRequireReceiver);
}
foreach (DamageType typeOfDamage in damageGiver.damageTypeDealt) {
if (!immuneTo.Contains (typeOfDamage)) {
if (weakness.Contains (typeOfDamage))
{
currentHP -= damageGiver.damageDealt *2;
}else{
currentHP -= damageGiver.damageDealt;}
StartCoroutine (CheckLife ());
if (this.tag == "Player")
{
var player = this.GetComponent<PlayerMovement>();
EventManager.BroadcastUnderAttack();
player.knockbackCount = player.knockbackTimer;
immune = true;
//Knockback
if(col.transform.position.x > player.transform.position.x)
{
player.knockFromRight = true;
}else{
player.knockFromRight = false;
}
}
}
}
}
}
}
IEnumerator CheckLife()
{
rend.material.color = Color.red;
yield return new WaitForSeconds (0.25f);
if (currentHP <= 0)
{
if (this.tag == "Enemy")
{
int dropAmount = Random.Range (0, 100);
//print (dropAmount);
if (dropAmount <= chanceToDrop) {
Instantiate (manaSource, this.transform.position, Quaternion.Euler (new Vector3 (0, 0, 0f)));
}
}
Destroy(this.gameObject);
if (this.tag == "Player")
{
EventManager.BroadcastPlayerKIA();
}
}
yield return new WaitForSeconds (0.25f);
rend.material.color = Color.white;
yield break;
}
As in the game you have more than one type of damage, it checks on the damage Dealer what kind of damage is doing to it, and then hurts the player. The Player gets immunity for a couple of seconds and if the player gets destroyd it sends an event about it and then comes this script
void HandleOnPlayerKIA ()
{
//print ("Lifes Remaining" + myStats.lifesRemaining);
myStats.lifesRemaining --;
myStats.UpdateTexts ();
if (myStats.lifesRemaining <= 0) {
LoadLevel ("TitleScreen");
myStats.lifesRemaining = 2;
} else {
LoadLevel (levelLoaded);
}
}
This script substract a life from the player and then if there are no more life remaining, it goes to TitleScreen or else reloads the where the player is.
This has been working for several enemys and levels, but then a boss appear, which shoots a laser beam. Which is a line renderer component with this scritp on it
private LineRenderer laser;
private BoxCollider2D box;
private float counter;
private float distance;
public Transform origin;
public Transform destination;
public float lineWidth = 2;
public float lineDrawSpeed = 6f;
public bool shooting;
// Use this for initialization
void Start () {
box = gameObject.AddComponent <BoxCollider2D>() as BoxCollider2D;
box.isTrigger = true;
box.enabled = false;
laser = GetComponent<LineRenderer> ();
laser.SetPosition (0, origin.position);
laser.SetWidth (2.0f, 2.0f);
distance = Vector3.Distance (origin.position, destination.position);
EventManager.OnPlayerKIA += HandleOnPlayerKIA;
}
void Update () {
if (shooting) {
box.enabled = true;
laser.enabled = true;
if (counter < distance) {
counter += .1f / lineDrawSpeed;
float x = Mathf.Lerp (0, distance, counter);
Vector3 pointA = origin.position;
Vector3 pointB = destination.position;
Vector3 pointAlongLine = x * Vector3.Normalize (pointB - pointA) + pointA;
laser.SetPosition (1, pointAlongLine);
box.transform.position = origin.position +(destination.position - origin.position)/ 2;
//box.transform.LookAt (origin.position);
box.size = new Vector2 ((destination.position - origin.position).magnitude, lineWidth);
}
} else {
box.enabled = false;
laser.enabled = false;
}
}
What it does is, when the line renderer is activated it also expands a box collider component to check if the player get's in the way. And then has a class called DamageSource which interacts with Damage Receiver.
So, to sum it up. The player gets killed twice by this enemy. That shouldn't happen. Any ideas? Help!? Been struggling with this for a couple of days by now, and don't know what to do.
I tried destroying the laser whean OnPlayerKIA, but didn't work. Tried to disable the laser component OnTriggerEnter if other collider's tag == "Player"... so far nothing worked...
Thanks in advance.