From ecaeb4d11f18c4e9b9bd8fdfd98ed8c15e39392c Mon Sep 17 00:00:00 2001 From: pbg2h23akl Date: Thu, 27 Jun 2024 10:27:18 +0200 Subject: [PATCH] Enemy takes Damage --- .../Assets/PrefabsKevin/Bullet/Bullet.prefab | 4 +-- .../Scripts/ScriptsKevin/BulletScript.cs | 25 ++++++++++++---- .../Assets/Scripts/ScriptsKevin/DataBullet.cs | 4 --- .../Assets/Scripts/ScriptsKevin/Enemy.cs | 29 ++++++++----------- .../EnemyScriptableObject.asset | 2 +- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Plunderblock/Assets/PrefabsKevin/Bullet/Bullet.prefab b/Plunderblock/Assets/PrefabsKevin/Bullet/Bullet.prefab index 16fda06..adf95bf 100644 --- a/Plunderblock/Assets/PrefabsKevin/Bullet/Bullet.prefab +++ b/Plunderblock/Assets/PrefabsKevin/Bullet/Bullet.prefab @@ -243,9 +243,9 @@ CapsuleCollider: m_Enabled: 1 serializedVersion: 2 m_Radius: 0.11 - m_Height: 1 + m_Height: 0.57 m_Direction: 0 - m_Center: {x: 0, y: 0, z: 0} + m_Center: {x: -0.08, y: 0, z: 0} --- !u!1 &5432457483690072105 GameObject: m_ObjectHideFlags: 0 diff --git a/Plunderblock/Assets/Scripts/ScriptsKevin/BulletScript.cs b/Plunderblock/Assets/Scripts/ScriptsKevin/BulletScript.cs index 7fc6137..e15621c 100644 --- a/Plunderblock/Assets/Scripts/ScriptsKevin/BulletScript.cs +++ b/Plunderblock/Assets/Scripts/ScriptsKevin/BulletScript.cs @@ -8,6 +8,10 @@ public class BulletScript : MonoBehaviour { public DataBullet dataBullet; + private float damage; + private float speed; + private int ammo; + public GameObject particalClash; public float lifetime = 3f; @@ -20,9 +24,6 @@ public class BulletScript : MonoBehaviour private Ray ray; private RaycastHit hit; - public void damageEnemy(float damageAmount){ - - } private void die(float lifetime){ if(time > lifetime){ @@ -34,17 +35,29 @@ public class BulletScript : MonoBehaviour direction = hit.point - transform.position;//Richtung in die, die Kugel fliegt } } - private void OnCollisionEnter(Collision collision){ - Debug.Log(collision.gameObject.name + " UND HIER IST DER TAG: " + collision.gameObject.tag); + public void clashPartcal(Collision collision){ ContactPoint contact = collision.GetContact(0); 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().takeDamage(damage); + } + clashPartcal(collision); GameObject.Destroy(this.gameObject); + } // Start is called before the first frame update void Start() { + damage = dataBullet.damage; + ammo = dataBullet.ammo; + speed = dataBullet.speed; + rb = this.GetComponent(); + ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f)); CheckForColliders(); @@ -54,7 +67,7 @@ public class BulletScript : MonoBehaviour // Update is called once per frame void Update() { - rb.velocity = direction.normalized * dataBullet.speed; + rb.velocity = direction.normalized * speed; die(lifetime); time += Time.deltaTime; } diff --git a/Plunderblock/Assets/Scripts/ScriptsKevin/DataBullet.cs b/Plunderblock/Assets/Scripts/ScriptsKevin/DataBullet.cs index ef26640..c0abfc1 100644 --- a/Plunderblock/Assets/Scripts/ScriptsKevin/DataBullet.cs +++ b/Plunderblock/Assets/Scripts/ScriptsKevin/DataBullet.cs @@ -11,8 +11,4 @@ public class DataBullet : ScriptableObject public float speed = 5f; public int ammo; - - public void setDamage(float damage){ - this.damage = damage; - } } diff --git a/Plunderblock/Assets/Scripts/ScriptsKevin/Enemy.cs b/Plunderblock/Assets/Scripts/ScriptsKevin/Enemy.cs index 584fb80..d3b7da2 100644 --- a/Plunderblock/Assets/Scripts/ScriptsKevin/Enemy.cs +++ b/Plunderblock/Assets/Scripts/ScriptsKevin/Enemy.cs @@ -8,30 +8,25 @@ public class Enemy : MonoBehaviour private bool alive = true; public DataEnemy dataEnemy; + private float health; + + private float damage; + + public void Start(){ + health = dataEnemy.health; + damage = dataEnemy.damage; + } + public void checkAlive(bool alive){ if(!alive){ GameObject.Destroy(this.gameObject); } } public void takeDamage(float bulletDamage){ - dataEnemy.health = dataEnemy.health - bulletDamage; - if(dataEnemy.health <= 0){ + health = health - bulletDamage; + if(health <= 0){ alive = false; + checkAlive(alive); } } - private void OnCollisionEnter(Collision collision){ - DataBullet bulletData = collision.gameObject.GetComponent(); - takeDamage(bulletData.damage); - checkAlive(alive); - } - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } } diff --git a/Plunderblock/Assets/Scripts/ScriptsKevin/ScriptableObjects/EnemyScriptableObject.asset b/Plunderblock/Assets/Scripts/ScriptsKevin/ScriptableObjects/EnemyScriptableObject.asset index fdc86cd..bae16bc 100644 --- a/Plunderblock/Assets/Scripts/ScriptsKevin/ScriptableObjects/EnemyScriptableObject.asset +++ b/Plunderblock/Assets/Scripts/ScriptsKevin/ScriptableObjects/EnemyScriptableObject.asset @@ -12,5 +12,5 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9a476e53f7c21af43b8fb3b00b60c337, type: 3} m_Name: EnemyScriptableObject m_EditorClassIdentifier: - health: 20 + health: 30 damage: 2