i3e-asg2/Assets/Scripts/Bullet.cs
Sc0rch-thinks b0364a3dbf final commit yay
commented abit then built the game
2024-07-05 23:30:15 +08:00

66 lines
1.8 KiB
C#

/*
*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
{
/// <summary>
/// the damage that the bullet will deal to the object that it collides with
/// </summary>
public int damage;
/// <summary>
/// how long the bullet will live for after it gets shot if it does not collide with a collider
/// </summary>
public float lifespan;
/// <summary>
/// when the bullet colides with annother object
/// </summary>
/// <param name="other">the collider of the other objectt</param>
void OnCollisionEnter(Collision other)
{
///<summary>
///checking if the other object is the player or enemy and then damage the appropriate object
///</summary>
if (other.transform.tag == "Player")
{
GameManager.instance.damagePlayer(damage);
}
else if (other.transform.tag == "enemy")
{
other.transform.GetComponent<EnemyAi>().health -= damage;
if (other.transform.GetComponent<EnemyAi>().health <= 0)
{
Destroy(other.gameObject);
}
}
killBullet();
}
/// <summary>
/// to calculate the time passed and kill the bullt afer a certain time
/// </summary>
void Update()
{
lifespan -= Time.deltaTime;
if (lifespan <= 0)
{
killBullet();
}
}
/// <summary>
/// to destroy the bullet
/// </summary>
public void killBullet()
{
Destroy(gameObject);
}
}