Question by
Ahri_Yuuki · Jan 10, 2017 at 08:43 PM ·
triggerdestroy
Whenever my player lands a shot on the enemy they hurt themselves, help please ><
Hello, I'm trying to make a space shooter game, made the health bar and connected it to the player. The enemies can hurt my player when they get on contact, but the player also gets hurt if one of the bolts get damaged >< please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyDestroyByContact : MonoBehaviour {
public GameObject explosion;
public PlayerHealth pHealth;
public int attackDamage;
private void Start()
{
pHealth = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
if (other.tag == "Bolt")
{
Destroy(other.gameObject);
}
if (other.tag == "Player" || pHealth.currentHealth > 0)
{
Attack();
}
}
void Attack()
{
pHealth.TakeDamage(attackDamage);
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612104545im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input to Destroy All objects in a Trigger 0 Answers
2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer
finding valid targets in range 1 Answer
Destroying GameObject after amount of triggers 2 Answers
Need help coding collision detection to a destroy command. 0 Answers