void OnTriggerEnter2D(Collider2D col) {
if (col.tag != "Enemy") {
if(col.tag == "Player" && player.canTakeDamage) {
player.currentHealth -= damageToPlayer - (player.defense * (damageToPlayer * (1/25))); //if so, subtract the damage from the player's health
player.StartCoroutine("InvincibleFrames"); //call the invincibility method, so we don't get spammed with damage
Destroy(gameObject);
} else {
Destroy(gameObject);
}
}
}
This fragment is supposed to ignore collisions on anything tagged “Enemy” and deal damage if it hits an object named “Player”. In the else statement, I want it to destroy itself if it hits anything else not tagged “Enemy” or “Player”. When it hits the player, it destroys itself, but when it hits anything else it just sits there until I run into it. Can anyone tell me what I’m doing wrong? There’s something I’m not understanding. Thanks
This should be simple debugging. Always use Debug.Log!
Try this and say what prints. Any issues just tell me as it is hand-typed.
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag != "Enemy")
{
if(col.tag == "Player")
{
// is player a reference to your player? Show me this code
if(player.canTakeDamage)
{
Debug.Log("player has taken " + damageToPlayer + " damage");
player.currentHealth -= damageToPlayer - (player.defense * (damageToPlayer * (1/25)));
player.StartCoroutine("InvincibleFrames");
Destroy(gameObject);
}
else
{
Debug.Log("player cannot take damage");
Destroy(gameObject);
}
}
else
{
Debug.Log("collision tag != enemy or Player");
}
}
else
{
Debug.Log("collision tag = 'Enemy'");
}
}
Code runs fine, but nothing debugs. I would assume that
Debug.Log("collision tag != enemy or Player");
should be what is debugged considering that the object isn’t hitting either of these, but it doesn’t.
I changed the code up a bit and added a certain Debug.Log section which acts interestingly
void OnTriggerEnter2D(Collider2D col) {
Debug.Log(col.tag);
if (col.tag != "Enemy") {
if(col.tag == "Player") {
if (player.canTakeDamage) {
player.currentHealth -= damageToPlayer - (player.defense * (damageToPlayer * (1/25))); //if so, subtract the damage from the player's health
player.StartCoroutine("InvincibleFrames"); //call the invincibility method, so we don't get spammed with damage
Destroy(gameObject);
} else {
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
} else {
Destroy(gameObject);
}
}
}
When the player collides, it debugs Player, but when it collides with the ground (where it gets stuck) it doesn’t debug Ground (the tag on the ground object), so this means that the trigger never hits the ground. There is no solid colliders or rigidbody on the projectile but it still sticks on the ground as if it had one. Ideas?
Take a look at this table at the bottom of the page:
So it would appear that no messages are returned when a static trigger collides with a static collider. Thanks for letting me know. I’ll just check for when the object stops moving and destroy it when it stops.