/*
*Author: Lin Hengrui Ryan
*Date:19/06/2024
*Description: for the bullet prefab to allow the bullet to deal damage to the player and enemies and to kill the bullet prefab after a certain duration or when it collides with a collider
*/
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class BulletCollider : MonoBehaviour
{
///
/// the damage that the bullet will deal to the object that it collides with
///
public int damage;
///
/// how long the bullet will live for after it gets shot if it does not collide with a collider
///
public float lifespan;
///
/// when the bullet colides with annother object
///
/// the collider of the other objectt
void OnCollisionEnter(Collision other)
{
///
///checking if the other object is the player or enemy and then damage the appropriate object
///
if (other.transform.tag == "Player")
{
GameManager.instance.damagePlayer(damage);
}
else if (other.transform.tag == "enemy")
{
other.transform.GetComponent().health -= damage;
if (other.transform.GetComponent().health <= 0)
{
Destroy(other.gameObject);
}
}
killBullet();
}
///
/// to calculate the time passed and kill the bullt afer a certain time
///
void Update()
{
lifespan -= Time.deltaTime;
if (lifespan <= 0)
{
killBullet();
}
}
///
/// to destroy the bullet
///
public void killBullet()
{
Destroy(gameObject);
}
}