From 9a70b0bd2a1f34ce3dba8665e58b6c216ecf2df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20P=C4=9Bnkava?= Date: Thu, 7 Aug 2025 02:02:46 +0200 Subject: [PATCH] Attacks on collisions --- .../Assets/Prefabs/PlayerContainer.prefab | 23 ++++++++ 3D blobici/Assets/Prefabs/enemy/Blob.prefab | 39 ++++++++++++++ .../Assets/Scripts/Enemy/EnemyAttack.cs | 52 +++++++++++++++++++ .../Assets/Scripts/Enemy/EnemyAttack.cs.meta | 2 + .../Assets/Scripts/HP/Health Manager.cs | 2 + 5 files changed, 118 insertions(+) create mode 100644 3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs create mode 100644 3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs.meta diff --git a/3D blobici/Assets/Prefabs/PlayerContainer.prefab b/3D blobici/Assets/Prefabs/PlayerContainer.prefab index d652522..784b80a 100644 --- a/3D blobici/Assets/Prefabs/PlayerContainer.prefab +++ b/3D blobici/Assets/Prefabs/PlayerContainer.prefab @@ -568,6 +568,7 @@ GameObject: - component: {fileID: 1960184128864250160} - component: {fileID: 1781483820777133190} - component: {fileID: 8149587721611830724} + - component: {fileID: 6867876445951065937} m_Layer: 0 m_Name: Player m_TagString: Player @@ -647,6 +648,28 @@ MonoBehaviour: _maxHealth: 100 _currentHealth: 100 _healthText: {fileID: 4603696628922152095} +--- !u!64 &6867876445951065937 +MeshCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3653993672432327505} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 1 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 5 + m_Convex: 1 + m_CookingOptions: 30 + m_Mesh: {fileID: 0} --- !u!1 &4992829604954765258 GameObject: m_ObjectHideFlags: 0 diff --git a/3D blobici/Assets/Prefabs/enemy/Blob.prefab b/3D blobici/Assets/Prefabs/enemy/Blob.prefab index fd244b3..afea0f3 100644 --- a/3D blobici/Assets/Prefabs/enemy/Blob.prefab +++ b/3D blobici/Assets/Prefabs/enemy/Blob.prefab @@ -12,6 +12,8 @@ GameObject: - component: {fileID: 4260072969707999758} - component: {fileID: 8136388527997030937} - component: {fileID: 8002523264253901019} + - component: {fileID: 5415715946912615516} + - component: {fileID: 146755941370531150} m_Layer: 0 m_Name: Blob m_TagString: Enemy @@ -104,6 +106,43 @@ MonoBehaviour: _maxHealth: 100 _currentHealth: 20 _healthText: {fileID: 0} +--- !u!114 &5415715946912615516 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2103289545128957355} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 333830e3ba10f2942b70a753c4c281de, type: 3} + m_Name: + m_EditorClassIdentifier: + attackRange: 5 + attackRate: 2 + attackDamage: 10 + attackAngle: 45 +--- !u!65 &146755941370531150 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2103289545128957355} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 1 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Size: {x: 4.4725122, y: 2.472512, z: 4.2375846} + m_Center: {x: 0, y: 0.6187928, z: 0} --- !u!1 &2485118932734020551 GameObject: m_ObjectHideFlags: 0 diff --git a/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs b/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs new file mode 100644 index 0000000..fdbb897 --- /dev/null +++ b/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs @@ -0,0 +1,52 @@ +using UnityEngine; + +public class EnemyAttack : MonoBehaviour +{ + + [SerializeField] private float attackRange = 5f; + [SerializeField] private float attackRate = 2f; + [SerializeField] private float attackDamage = 10f; + [SerializeField] private float attackAngle = 45f; + + private float lastAttackTime = 0f; + + // Start is called once before the first execution of Update after the MonoBehaviour is created + void Start() + { + } + + // Update is called once per frame + void Update() + { + } + + private void FixedUpdate() + { + + } + + private void OnTriggerEnter(Collider other) + { + + } + + private void OnTriggerStay(Collider other) + { + var player = other.GetComponent(); + + if (player != null && Time.time - lastAttackTime > attackRate) + { + var health = other.GetComponent(); + if (health != null) + { + health.ModifyHealth(-attackDamage); + Debug.Log("Enemy attacked! Next attack in: " + (1f / attackRate) + " seconds"); + lastAttackTime = Time.time; + } + } + } + + private void OnTriggerExit(Collider other) + { + } +} diff --git a/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs.meta b/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs.meta new file mode 100644 index 0000000..3a961dc --- /dev/null +++ b/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 333830e3ba10f2942b70a753c4c281de \ No newline at end of file diff --git a/3D blobici/Assets/Scripts/HP/Health Manager.cs b/3D blobici/Assets/Scripts/HP/Health Manager.cs index da83194..b1641dc 100644 --- a/3D blobici/Assets/Scripts/HP/Health Manager.cs +++ b/3D blobici/Assets/Scripts/HP/Health Manager.cs @@ -55,6 +55,8 @@ public class HealthManager : MonoBehaviour return; } _currentHealth += amount; + + Debug.Log("Health of " + gameObject.name + ": " + _currentHealth); } }