43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
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);
|
||
}
|
||
}
|
||
|
||
|