i3e-asg2/Assets/Scripts/Bullet.cs
2024-07-02 10:42:09 +08:00

40 lines
871 B
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class BulletCollider : MonoBehaviour
{
public int damage;
public float lifespan;
void OnCollisionEnter(Collision other)
{
Debug.Log("nf");
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);
}
}
}
void Update()
{
lifespan-=Time.deltaTime;
if(lifespan<=0)
{
killBullet();
}
}
public void killBullet()
{
Destroy(gameObject);
}
}