MIF_E31221325/Assets/Sprites/Space Shooter Template FREE/Scripts/Projectile.cs

43 lines
1.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Defines the damage and defines whether the projectile belongs to the Enemy or to the Player, whether the projectile is destroyed in the collision, or not and amount of damage.
/// </summary>
public class Projectile : MonoBehaviour {
[Tooltip("Damage which a projectile deals to another object. Integer")]
public int damage;
[Tooltip("Whether the projectile belongs to the Enemy or to the Player")]
public bool enemyBullet;
[Tooltip("Whether the projectile is destroyed in the collision, or not")]
public bool destroyedByCollision;
private void OnTriggerEnter2D(Collider2D collision) //when a projectile collides with another object
{
if (enemyBullet && collision.tag == "Player") //if anoter object is 'player' or 'enemy sending the command of receiving the damage
{
Player.instance.GetDamage(damage);
if (destroyedByCollision)
Destruction();
}
else if (!enemyBullet && collision.tag == "Enemy")
{
collision.GetComponent<Enemy>().GetDamage(damage);
if (destroyedByCollision)
Destruction();
}
}
void Destruction()
{
Destroy(gameObject);
}
}