From e397d13f41881c8cba4b71ff7facf94ff9ac12a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20P=C4=9Bnkava?= Date: Sat, 6 Sep 2025 00:44:27 +0200 Subject: [PATCH] Collisions, AI tweaks --- .../Assets/Prefabs/Scripts/RoomHandler.cs | 6 +- 3D blobici/Assets/Prefabs/enemy/Blob.prefab | 89 +++++++------------ .../Assets/Scripts/Enemy/EnemyAttack.cs | 19 ++-- .../Assets/Scripts/Enemy/EnemyMovement.cs | 70 ++++++++------- .../Assets/Scripts/MapGen/MapGenManager.cs | 1 - 5 files changed, 78 insertions(+), 107 deletions(-) diff --git a/3D blobici/Assets/Prefabs/Scripts/RoomHandler.cs b/3D blobici/Assets/Prefabs/Scripts/RoomHandler.cs index 9fec8b2..58c2d32 100644 --- a/3D blobici/Assets/Prefabs/Scripts/RoomHandler.cs +++ b/3D blobici/Assets/Prefabs/Scripts/RoomHandler.cs @@ -103,11 +103,7 @@ public class RoomHandler : MonoBehaviour agent.Warp(spawnPosition); } - if (agent.isOnNavMesh) - { - Debug.Log("Enemy successfully placed on NavMesh"); - } - else + if (!agent.isOnNavMesh) { Debug.LogWarning("Enemy could not be placed on NavMesh"); } diff --git a/3D blobici/Assets/Prefabs/enemy/Blob.prefab b/3D blobici/Assets/Prefabs/enemy/Blob.prefab index 8e03961..20559ba 100644 --- a/3D blobici/Assets/Prefabs/enemy/Blob.prefab +++ b/3D blobici/Assets/Prefabs/enemy/Blob.prefab @@ -9,15 +9,14 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1593526044607189551} - - component: {fileID: 4260072969707999758} - component: {fileID: 8136388527997030937} - component: {fileID: 8002523264253901019} - component: {fileID: 5415715946912615516} - - component: {fileID: 146755941370531150} - component: {fileID: -7480623267348950463} + - component: {fileID: 7186747264596157612} m_Layer: 7 m_Name: Blob - m_TagString: Enemy + m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -39,31 +38,6 @@ Transform: - {fileID: 4728640573077846392} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!143 &4260072969707999758 -CharacterController: - 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_ProvidesContacts: 0 - m_Enabled: 1 - serializedVersion: 3 - m_Height: 1 - m_Radius: 1 - m_SlopeLimit: 5 - m_StepOffset: 0.3 - m_SkinWidth: 0.08 - m_MinMoveDistance: 0.001 - m_Center: {x: 0, y: 0.5, z: 0} --- !u!114 &8136388527997030937 MonoBehaviour: m_ObjectHideFlags: 0 @@ -80,7 +54,6 @@ MonoBehaviour: player: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3} updatePathInterval: 0.5 roomCheckInterval: 2 - attackRange: 3 sightRange: 50 patrolRange: 5 enemyAttack: {fileID: 5415715946912615516} @@ -112,15 +85,37 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 333830e3ba10f2942b70a753c4c281de, type: 3} m_Name: m_EditorClassIdentifier: - attackRange: 3 + attackRange: 2.75 attackRate: 2 attackDamage: 10 attackAngle: 45 player: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3} animator: {fileID: 0} agent: {fileID: -7480623267348950463} ---- !u!65 &146755941370531150 -BoxCollider: +--- !u!195 &-7480623267348950463 +NavMeshAgent: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2103289545128957355} + m_Enabled: 1 + m_AgentTypeID: 0 + m_Radius: 1 + m_Speed: 7 + m_Acceleration: 50 + avoidancePriority: 50 + m_AngularSpeed: 120 + m_StoppingDistance: 0 + m_AutoTraverseOffMeshLink: 1 + m_AutoBraking: 0 + m_AutoRepath: 1 + m_Height: 2 + m_BaseOffset: 0 + m_WalkableMask: 4294967295 + m_ObstacleAvoidanceType: 4 +--- !u!136 &7186747264596157612 +CapsuleCollider: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} @@ -137,31 +132,11 @@ BoxCollider: m_IsTrigger: 0 m_ProvidesContacts: 0 m_Enabled: 1 - serializedVersion: 3 - m_Size: {x: 2, y: 1, z: 2} - m_Center: {x: 0, y: 0.6187928, z: 0} ---- !u!195 &-7480623267348950463 -NavMeshAgent: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2103289545128957355} - m_Enabled: 1 - m_AgentTypeID: 0 - m_Radius: 0.5 - m_Speed: 3.5 - m_Acceleration: 8 - avoidancePriority: 50 - m_AngularSpeed: 120 - m_StoppingDistance: 0 - m_AutoTraverseOffMeshLink: 1 - m_AutoBraking: 1 - m_AutoRepath: 1 - m_Height: 2 - m_BaseOffset: 0 - m_WalkableMask: 4294967295 - m_ObstacleAvoidanceType: 4 + serializedVersion: 2 + m_Radius: 1 + m_Height: 1 + m_Direction: 1 + m_Center: {x: 0, y: 0, 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 index 544184f..eb6f6ef 100644 --- a/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs +++ b/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs @@ -52,6 +52,8 @@ public class EnemyAttack : MonoBehaviour } } + public float GetAttackRange() => attackRange; + // Metoda pro pokus o útok - volána z EnemyMovement public bool TryAttack() { @@ -63,19 +65,17 @@ public class EnemyAttack : MonoBehaviour // Kontrola vzdálenosti a úhlu k hráči float distanceToPlayer = Vector3.Distance(transform.position, player.position); - bool inRange = distanceToPlayer <= attackRange; + bool inRange = IsPlayerInAttackRange(); bool inAngle = IsPlayerInAttackAngle(); bool cooldownReady = Time.time - lastAttackTime >= attackRate; - Debug.Log($"TryAttack: range={inRange}, angle={inAngle}, cooldown={cooldownReady}, distance={distanceToPlayer}"); - if (inRange && cooldownReady) { StartCoroutine(PerformAttack()); return true; } - Debug.Log("Attack conditions not met"); + Debug.Log("Attack conditions not met. Conditions: range: " + inRange + "angle: " + inAngle + "cooldown: " + cooldownReady); return false; } @@ -100,10 +100,9 @@ public class EnemyAttack : MonoBehaviour if (player == null) return false; float distance = Vector3.Distance(transform.position, player.position); - bool inRange = distance <= attackRange * 1.1f; - bool inAngle = IsPlayerInAttackAngle(); + bool inRange = distance <= attackRange * 1.2f; - return inRange && inAngle; + return inRange; } private IEnumerator PerformAttack() @@ -126,7 +125,7 @@ public class EnemyAttack : MonoBehaviour } // Počkej chvíli před aplikováním poškození - yield return new WaitForSeconds(0.3f); + yield return new WaitForSeconds(0.1f); bool playerDashing = skillHandler.IsDashing(); // Aplikuj poškození, pokud je hráč stále v dosahu @@ -141,7 +140,7 @@ public class EnemyAttack : MonoBehaviour } // Počkej na dokončení animace - yield return new WaitForSeconds(0.7f); + yield return new WaitForSeconds(0.3f); // Obnov pohyb pouze pokud nebyl původně zastavený if (!wasStopped) @@ -173,7 +172,7 @@ public class EnemyAttack : MonoBehaviour // Public metody pro komunikaci s EnemyMovement public bool CanAttack() { - return canAttack && !isAttacking; + return canAttack && !isAttacking && IsPlayerInAttackRange(); } diff --git a/3D blobici/Assets/Scripts/Enemy/EnemyMovement.cs b/3D blobici/Assets/Scripts/Enemy/EnemyMovement.cs index d3caea9..87ceb39 100644 --- a/3D blobici/Assets/Scripts/Enemy/EnemyMovement.cs +++ b/3D blobici/Assets/Scripts/Enemy/EnemyMovement.cs @@ -11,7 +11,6 @@ public class EnemyMovement : MonoBehaviour public float roomCheckInterval = 2f; [Header("Combat")] - public float attackRange = 2f; public float sightRange = 20f; public float patrolRange = 5f; @@ -24,6 +23,7 @@ public class EnemyMovement : MonoBehaviour private Vector3 patrolTarget; private float lastStateChangeTime; private float minAttackStateDuration = 1.0f; + private float attackRange; [Header("Animation")] public Animator animator; @@ -32,6 +32,8 @@ public class EnemyMovement : MonoBehaviour { // Najdi hráče GameObject playerObject = GameObject.FindWithTag("Player"); + attackRange = enemyAttack.GetAttackRange(); + if (playerObject != null) { player = playerObject.transform; @@ -148,13 +150,13 @@ public class EnemyMovement : MonoBehaviour float distanceToPlayer = Vector3.Distance(transform.position, player.position); // Zůstaň v attack stavu pokud může útočit nebo se otáčí k hráči - if (!attackAttempted && enemyAttack.CanAttack() && enemyAttack.IsPlayerInAttackRange()) + if (attackAttempted && enemyAttack.CanAttack()) { return; } // Pokud je hráč příliš daleko, pokračuj v pronásledování - if (distanceToPlayer > attackRange * 1.5f && !enemyAttack.IsAttacking()) + if (distanceToPlayer > attackRange && !enemyAttack.IsAttacking()) { ChangeState(EnemyState.Chasing); } @@ -182,7 +184,7 @@ public class EnemyMovement : MonoBehaviour } } - yield return new WaitForSeconds(0.3f); + yield return new WaitForSeconds(0.1f); } } @@ -269,39 +271,39 @@ public class EnemyMovement : MonoBehaviour /*====== FOR DEBUGGING PURPOSES ONLY ========== */ - private void OnDrawGizmosSelected() - { - Gizmos.color = Color.red; - Gizmos.DrawWireSphere(transform.position, attackRange); + //private void OnDrawGizmosSelected() + //{ + // Gizmos.color = Color.red; + // Gizmos.DrawWireSphere(transform.position, attackRange); - Gizmos.color = Color.yellow; - Gizmos.DrawWireSphere(transform.position, sightRange); + // Gizmos.color = Color.yellow; + // Gizmos.DrawWireSphere(transform.position, sightRange); - Gizmos.color = Color.blue; - Gizmos.DrawWireSphere(patrolCenter, patrolRange); + // Gizmos.color = Color.blue; + // Gizmos.DrawWireSphere(patrolCenter, patrolRange); - if (Application.isPlaying) - { - Gizmos.color = Color.green; - Gizmos.DrawSphere(patrolTarget, 0.3f); - Gizmos.DrawLine(transform.position, patrolTarget); - } - } + // if (Application.isPlaying) + // { + // Gizmos.color = Color.green; + // Gizmos.DrawSphere(patrolTarget, 0.3f); + // Gizmos.DrawLine(transform.position, patrolTarget); + // } + //} - private void OnGUI() - { - if (Application.isPlaying && enemyAttack != null) - { - GUI.Label(new Rect(10, 30, 300, 20), $"State: {currentState}"); - GUI.Label(new Rect(10, 50, 300, 20), $"CanAttack: {enemyAttack.CanAttack()}"); - GUI.Label(new Rect(10, 70, 300, 20), $"IsAttacking: {enemyAttack.IsAttacking()}"); - GUI.Label(new Rect(10, 90, 300, 20), $"Cooldown: {enemyAttack.GetCooldownProgress():P0}"); + //private void OnGUI() + //{ + // if (Application.isPlaying && enemyAttack != null) + // { + // GUI.Label(new Rect(10, 30, 300, 20), $"State: {currentState}"); + // GUI.Label(new Rect(10, 50, 300, 20), $"CanAttack: {enemyAttack.CanAttack()}"); + // GUI.Label(new Rect(10, 70, 300, 20), $"IsAttacking: {enemyAttack.IsAttacking()}"); + // GUI.Label(new Rect(10, 90, 300, 20), $"Cooldown: {enemyAttack.GetCooldownProgress():P0}"); - if (player != null) - { - float distance = Vector3.Distance(transform.position, player.position); - GUI.Label(new Rect(10, 110, 300, 20), $"Distance: {distance:F2}"); - } - } - } + // if (player != null) + // { + // float distance = Vector3.Distance(transform.position, player.position); + // GUI.Label(new Rect(10, 110, 300, 20), $"Distance: {distance:F2}"); + // } + // } + //} } \ No newline at end of file diff --git a/3D blobici/Assets/Scripts/MapGen/MapGenManager.cs b/3D blobici/Assets/Scripts/MapGen/MapGenManager.cs index e123016..570d5c4 100644 --- a/3D blobici/Assets/Scripts/MapGen/MapGenManager.cs +++ b/3D blobici/Assets/Scripts/MapGen/MapGenManager.cs @@ -276,7 +276,6 @@ public class MapGenManager : MonoBehaviour // Bake globálnĂ­ NavMesh globalNavMeshSurface.BuildNavMesh(); - Debug.Log("Global NavMesh baked"); } /* ============================================================ */