Issue resolved myself
I am having an issue with a collision that used to work but now for some reason will not detect. Please help.
I have been working on a 2D platform game and I have a player damage script which links into the relevant animations and reduces health etc. This was working absolutely perfectly until I tried to add a lives system following a tutorial. Since then the final collision to play the death animation simply will not register and I have no idea why. I have removed all the extra scripts and objects that were added and my damage script is now exactly as it was when it was working but the final collision to make health go from 1 to 0 won't work. The trigger in the animator works as I can click it and the animation plays fine and if I script for the Die function to be called automatically this also works but the player dies without needing a collision. I have tried using Debug.Log and this shows that the collision is not detecting but I am completely lost as to why not and would really appreciate any help which could be offered. Below is the script for damage.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDamage : MonoBehaviour
{
// private LevelManager levelManager;
private NewPlayControl player;
public int health;
public int currentHealth;
public int damage = 1;
public bool BeenDamaged = false;
public bool IsDead = false;
public bool knockfromRight;
public bool knockfromLeft;
public float knockback;
public CircleCollider2D enemycollider;
public CapsuleCollider2D playercollider;
public Transform rightCheck;
public Transform leftCheck;
float hitRadius = 0.1f;
public LayerMask whatisEnemy;
public int Lives;
public int currentLives;
// Use this for initialization
void Start ()
{
// levelManager = FindObjectOfType<LevelManager>();
player = FindObjectOfType<NewPlayControl>();
currentHealth = health;
playercollider = GameObject.Find("SuperNoCollider").GetComponent<CapsuleCollider2D>();
enemycollider = GameObject.Find("EnemyGround").GetComponent<CircleCollider2D>();
currentLives = Lives;
}
void TakeDamage()
{
currentHealth = (currentHealth - damage);
}
void HitDirection()//This determines which side is colliding with enemy
{
knockfromRight = Physics2D.OverlapCircle(rightCheck.position, hitRadius, whatisEnemy);
knockfromLeft = Physics2D.OverlapCircle(leftCheck.position, hitRadius, whatisEnemy);
}
IEnumerator OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
if (player.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
Physics2D.IgnoreCollision(playercollider, enemycollider, true);
}
else if (!BeenDamaged && !IsDead && currentHealth > 1)
{
HitDirection();
if (knockfromLeft)
{
player.anim.SetTrigger("LTakeDamage");
knockfromLeft = true;
player.rb.AddForce(transform.right * knockback);
player.rb.AddForce(transform.up * knockback);
player.CanControl = false;
yield return new WaitForSeconds(1);
knockfromLeft = false;
player.CanControl = true;
TakeDamage();
BeenDamaged = true;
yield return new WaitForSeconds(3);
BeenDamaged = false;
}
else if (knockfromRight)
{
player.anim.SetTrigger("RTakeDamage");
knockfromRight = true;
player.rb.AddForce(transform.right * -knockback);
player.rb.AddForce(transform.up * knockback);
player.CanControl = false;
yield return new WaitForSeconds(1);
knockfromRight = false;
player.CanControl = true;
TakeDamage();
BeenDamaged = true;
yield return new WaitForSeconds(3);
BeenDamaged = false;
}
else if (!BeenDamaged && !IsDead && currentHealth == 1)
{
TakeDamage();
}
if (currentHealth == 0)
{
Die();
}
}
}
}
public void Die()
{
BeenDamaged = false;
IsDead = true;
player.anim.SetTrigger("Die");
if (IsDead == true)
{
player.CanControl = false;
playercollider.enabled = false;
player.rb.gravityScale = 0;
}
}
}
PS - I have also noticed that the CapsuleCollider2D is being removed from my player when I press play in the editor for some unknown reason and I am not sure if this is the issue as it stills registers the first collisions.][1]