Okay, so I am making a 2D game and I am trying to make a shield to block the enemy’s bullet, but there is a problem, the shield is a child to the player object, and I have a script in the player object that says whenever the player gets touched by the enemy’s bullet then the player would take damage, that’s why whenever the enemy’s bullet touches the shield, the player still takes damage, so I don’t know what to do, if I un-child the shield from the player then it won’t follow or rotate around the player
here's the player's health script
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public GameObject backgroundMusic;
public GameObject theDeathEffect;
public GameObject GameOverCanvas;
public int DamageTaken = 20;
public HealthBar healthBar;
public GameObject Player;
public GameObject Gun;
public GameObject projectile;
public GameObject RespawnSound;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("EnemyBullet"))
{
TakeDamage(DamageTaken);
healthBar.SetHealth(currentHealth);
}
if(currentHealth <= 0)
{
KillPlayer();
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
}
void KillPlayer()
{
Time.timeScale = 0;
GameOverCanvas.SetActive(true);
Player.SetActive(false);
Gun.SetActive(false);
projectile.SetActive(false);
RespawnSound.SetActive(true);
ScoreScript.scoreValue = 0;
}
}
here’s the enemy’s bullet script
public class Projectile : MonoBehaviour
{
public float speed;
public GameObject CollisionEffect;
private Transform Obstacles;
private Transform player;
private Vector2 target;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player").transform;
Obstacles = GameObject.FindWithTag("Obstacles").transform;
target = new Vector2(player.position.x, player.position.y);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
if (transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
DestroyProjectile();
}
if(other.CompareTag("Obstacles"))
{
DestroyProjectile();
}
if (other.CompareTag("Shield"))
{
DestroyProjectile();
}
}
void DestroyProjectile ()
{
Destroy(gameObject);
Instantiate(CollisionEffect, transform.position, Quaternion.identity);
}
}
help would be really appreciated