From bff2df154a6caee63a883e4de1796901a1aa4043 Mon Sep 17 00:00:00 2001 From: Felix Boucher Date: Sun, 5 Nov 2023 11:52:48 -0500 Subject: [PATCH 1/2] fix detection range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit besoin : - quand une unité meurt, la prochaine unité n'est pas targetted solution : - deactivate and reactivate collider to re-trigger collision --- Assets/Scripts/Detection.cs | 2 ++ Assets/Scripts/PlayerAction/ClickBehavior.cs | 17 ++++++++++++++++- Assets/Scripts/Tiles/SpawnerTile.cs | 10 ++++++++++ Assets/Tiles/EnemySpawner.asset | 1 + Assets/Tiles/EnemySpawnerStart.asset | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index 8b55fd6..c3acfcf 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -103,6 +103,8 @@ public class Detection : MonoBehaviour if(_projectileDamage == 0) { if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) { _entityLinked.IsEnemyDetected = false; + enabled = false; + enabled = true; } } } diff --git a/Assets/Scripts/PlayerAction/ClickBehavior.cs b/Assets/Scripts/PlayerAction/ClickBehavior.cs index d5ffb42..c5e15fc 100644 --- a/Assets/Scripts/PlayerAction/ClickBehavior.cs +++ b/Assets/Scripts/PlayerAction/ClickBehavior.cs @@ -11,9 +11,10 @@ public class ClickBehavior : MonoBehaviour private void Update() { + Vector2 clickPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); + if (Input.GetMouseButton(0)) { - Vector2 clickPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); List listColliders = new(Physics2D.OverlapCircleAll(clickPoint, 0.05f)); List resourceColliders = listColliders.FindAll(obj => obj.CompareTag("Resource")); if (resourceColliders.Count > 0) @@ -25,6 +26,20 @@ public class ClickBehavior : MonoBehaviour } } +#if UNITY_EDITOR + if (Input.GetMouseButtonUp(0)) + { + const float clickRangeForSpawn = 0.7f; + var spawners = LevelManager.Instance.GetAll(); + var closest = spawners.Minimum(spawn => Vector2.Distance(spawn.Position, transform.position)); + var distance = Vector2.Distance(clickPoint, closest.Position); + + if (distance < clickRangeForSpawn) + { + closest.TriggerSpawnDebug(); + } + } +#endif } public void ChangeGameSpeed() diff --git a/Assets/Scripts/Tiles/SpawnerTile.cs b/Assets/Scripts/Tiles/SpawnerTile.cs index b37aa4f..db097ca 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -136,4 +136,14 @@ public class SpawnerTile : LevelTile { enemy.Create(Position, parent: LevelManager.Instance.LevelTransform); } + +#if UNITY_EDITOR + [Header("DEBUG")] + [SerializeField] + private GameObject _debugPrefab; + public void TriggerSpawnDebug() + { + _debugPrefab.Create(Position, parent: LevelManager.Instance.LevelTransform); + } +#endif } \ No newline at end of file diff --git a/Assets/Tiles/EnemySpawner.asset b/Assets/Tiles/EnemySpawner.asset index d63a169..3364c50 100644 --- a/Assets/Tiles/EnemySpawner.asset +++ b/Assets/Tiles/EnemySpawner.asset @@ -18,3 +18,4 @@ MonoBehaviour: _spawnOnStart: 0 _spawnSpeed: 0.02 _spawnCounter: 0 + _debugPrefab: {fileID: 80204295746100150, guid: 2419a879bd4e47d4fa8b30de0fcdde42, type: 3} diff --git a/Assets/Tiles/EnemySpawnerStart.asset b/Assets/Tiles/EnemySpawnerStart.asset index 6564483..9d9c1c2 100644 --- a/Assets/Tiles/EnemySpawnerStart.asset +++ b/Assets/Tiles/EnemySpawnerStart.asset @@ -18,3 +18,4 @@ MonoBehaviour: _spawnOnStart: 1 _spawnSpeed: 0 _spawnCounter: 0 + _debugPrefab: {fileID: 80204295746100150, guid: 2419a879bd4e47d4fa8b30de0fcdde42, type: 3} From 4568c350bfec91842dc9a8fe0917798fbfce7dda Mon Sep 17 00:00:00 2001 From: Felix Boucher Date: Sun, 12 Nov 2023 20:31:21 -0500 Subject: [PATCH 2/2] toggle collider in coroutine instead of script --- Assets/Scripts/Detection.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index c3acfcf..5c1339f 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -103,14 +103,19 @@ public class Detection : MonoBehaviour if(_projectileDamage == 0) { if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) { _entityLinked.IsEnemyDetected = false; - enabled = false; - enabled = true; + StartCoroutine(ToggleCollider()); } } } } + IEnumerator ToggleCollider() + { + _collider.enabled = false; + yield return null; + _collider.enabled = true; + } //Getter and Setter public Entity EntityLinked