Enemy takes Damage

This commit is contained in:
pbg2h23akl 2024-06-27 10:27:18 +02:00
parent 36d4d1c9b6
commit ecaeb4d11f
5 changed files with 34 additions and 30 deletions

View File

@ -243,9 +243,9 @@ CapsuleCollider:
m_Enabled: 1 m_Enabled: 1
serializedVersion: 2 serializedVersion: 2
m_Radius: 0.11 m_Radius: 0.11
m_Height: 1 m_Height: 0.57
m_Direction: 0 m_Direction: 0
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: -0.08, y: 0, z: 0}
--- !u!1 &5432457483690072105 --- !u!1 &5432457483690072105
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -8,6 +8,10 @@ public class BulletScript : MonoBehaviour
{ {
public DataBullet dataBullet; public DataBullet dataBullet;
private float damage;
private float speed;
private int ammo;
public GameObject particalClash; public GameObject particalClash;
public float lifetime = 3f; public float lifetime = 3f;
@ -20,9 +24,6 @@ public class BulletScript : MonoBehaviour
private Ray ray; private Ray ray;
private RaycastHit hit; private RaycastHit hit;
public void damageEnemy(float damageAmount){
}
private void die(float lifetime){ private void die(float lifetime){
if(time > lifetime){ if(time > lifetime){
@ -34,18 +35,30 @@ public class BulletScript : MonoBehaviour
direction = hit.point - transform.position;//Richtung in die, die Kugel fliegt direction = hit.point - transform.position;//Richtung in die, die Kugel fliegt
} }
} }
private void OnCollisionEnter(Collision collision){ public void clashPartcal(Collision collision){
Debug.Log(collision.gameObject.name + " UND HIER IST DER TAG: " + collision.gameObject.tag);
ContactPoint contact = collision.GetContact(0); ContactPoint contact = collision.GetContact(0);
Instantiate(particalClash,transform.position,Quaternion.LookRotation(contact.normal)); Instantiate(particalClash,transform.position,Quaternion.LookRotation(contact.normal));
}
private void OnCollisionEnter(Collision collision){
Debug.Log(collision.gameObject.name + " UND HIER IST DER TAG: " + collision.gameObject.tag);
if(collision.transform.tag == "Enemy"){
collision.transform.GetComponent<Enemy>().takeDamage(damage);
}
clashPartcal(collision);
GameObject.Destroy(this.gameObject); GameObject.Destroy(this.gameObject);
} }
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
damage = dataBullet.damage;
ammo = dataBullet.ammo;
speed = dataBullet.speed;
rb = this.GetComponent<Rigidbody>(); rb = this.GetComponent<Rigidbody>();
ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f)); ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f));
CheckForColliders(); CheckForColliders();
} }
@ -54,7 +67,7 @@ public class BulletScript : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
rb.velocity = direction.normalized * dataBullet.speed; rb.velocity = direction.normalized * speed;
die(lifetime); die(lifetime);
time += Time.deltaTime; time += Time.deltaTime;
} }

View File

@ -11,8 +11,4 @@ public class DataBullet : ScriptableObject
public float speed = 5f; public float speed = 5f;
public int ammo; public int ammo;
public void setDamage(float damage){
this.damage = damage;
}
} }

View File

@ -8,30 +8,25 @@ public class Enemy : MonoBehaviour
private bool alive = true; private bool alive = true;
public DataEnemy dataEnemy; public DataEnemy dataEnemy;
private float health;
private float damage;
public void Start(){
health = dataEnemy.health;
damage = dataEnemy.damage;
}
public void checkAlive(bool alive){ public void checkAlive(bool alive){
if(!alive){ if(!alive){
GameObject.Destroy(this.gameObject); GameObject.Destroy(this.gameObject);
} }
} }
public void takeDamage(float bulletDamage){ public void takeDamage(float bulletDamage){
dataEnemy.health = dataEnemy.health - bulletDamage; health = health - bulletDamage;
if(dataEnemy.health <= 0){ if(health <= 0){
alive = false; alive = false;
checkAlive(alive);
} }
} }
private void OnCollisionEnter(Collision collision){
DataBullet bulletData = collision.gameObject.GetComponent<DataBullet>();
takeDamage(bulletData.damage);
checkAlive(alive);
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
} }

View File

@ -12,5 +12,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9a476e53f7c21af43b8fb3b00b60c337, type: 3} m_Script: {fileID: 11500000, guid: 9a476e53f7c21af43b8fb3b00b60c337, type: 3}
m_Name: EnemyScriptableObject m_Name: EnemyScriptableObject
m_EditorClassIdentifier: m_EditorClassIdentifier:
health: 20 health: 30
damage: 2 damage: 2