Bullet und health script

This commit is contained in:
2026-08-20 09:52:06 +02:00
parent c320f54ec9
commit cca8e87739
3 changed files with 40 additions and 5 deletions
+12 -5
View File
@@ -1,32 +1,39 @@
//Oliver //Oliver
using UnityEngine; using UnityEngine;
public class Bullet : MonoBehaviour public class ARBullet : MonoBehaviour
{ {
public float speed; public float speed;
public int damage; public int damage = 20;
private Vector3 position = Vector3.forward; private Vector3 position = Vector3.forward;
private Rigidbody rb; private Rigidbody rb;
void Start() void Start()
{ {
GetComponent<Rigidbody>(); GetComponent<Rigidbody>();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
transform.Translate(position.normalized * speed * Time.deltaTime); transform.Translate(position.normalized * speed * Time.deltaTime);
} }
private void OnCollisionEnter(Collision collision) private void OnCollisionEnter(Collision collision)
{ {
if (collision.gameObject.tag == "Enemy") if (collision.gameObject.tag == "Enemy")
{ {
Destroy(collision.gameObject); EnemyHealth enemy = collision.gameObject.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
Destroy(gameObject);
} }
else if(collision.gameObject.tag == "Wall") else if(collision.gameObject.tag == "Wall")
{ {
Destroy(this); Destroy(gameObject);
} }
} }
+26
View File
@@ -0,0 +1,26 @@
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int maxHP = 100;
private int curHP;
void Start()
{
curHP = maxHP;
}
public void TakeDamage(int damageAmount)
{
curHP -= damageAmount;
if (curHP <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e2665e725aa6c7642bb84ea1a671970a