From 167a7a3e4c0d811292ec1867f48cc2c4fcf16204 Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:02:22 +0200 Subject: [PATCH] Refactored Claudia.cs & fixed a bug --- Assets/game/Scenes/level2.unity | 8 -- Assets/game/enemy/Claudia/Scripts/Claudia.cs | 89 ++++++++++---------- Assets/game/enemy/Justin/Justin.prefab | 1 + Assets/game/enemy/Justin/Scripts/Justin.cs | 55 +++++++----- Assets/game/enemy/Zoltan/Scripts/Zoltan.cs | 55 +++++++----- Assets/game/enemy/Zoltan/Zoltan.prefab | 1 + 6 files changed, 117 insertions(+), 92 deletions(-) diff --git a/Assets/game/Scenes/level2.unity b/Assets/game/Scenes/level2.unity index 8e42bcc..6a08181 100644 --- a/Assets/game/Scenes/level2.unity +++ b/Assets/game/Scenes/level2.unity @@ -29133,10 +29133,6 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 3650605710375081848, guid: ad180bd9dbc48b24cb65ac52928f9626, type: 3} - propertyPath: playerAnimator - value: - objectReference: {fileID: 820667358} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: ad180bd9dbc48b24cb65ac52928f9626, type: 3} --- !u!4 &1392712637 stripped @@ -32266,10 +32262,6 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 8174632731044781324, guid: 212221bc4a138cd419eb6cc39547b1cb, type: 3} - propertyPath: playerAnimator - value: - objectReference: {fileID: 820667358} - target: {fileID: 8496836896711832325, guid: 212221bc4a138cd419eb6cc39547b1cb, type: 3} propertyPath: m_Name value: Justin diff --git a/Assets/game/enemy/Claudia/Scripts/Claudia.cs b/Assets/game/enemy/Claudia/Scripts/Claudia.cs index 9810a49..21e2574 100644 --- a/Assets/game/enemy/Claudia/Scripts/Claudia.cs +++ b/Assets/game/enemy/Claudia/Scripts/Claudia.cs @@ -5,40 +5,41 @@ using Pathfinding; public class Claudia : MonoBehaviour { - - public Transform target; + [Header("Speed & Range")] public float speed = 200f; + public float range = 10f; + + [Header("Pathfinding")] public float nextWaypointDistance = 3f; - public float range = 10f; // the range at which the enemy will start moving towards the player - float distance; - public Transform enemyGFX; - private Transform player; // reference to the player's transform - Path path; - int currentWaypoint = 0; - Seeker seeker; + Transform player; Rigidbody2D rb; - public Animator animator; - bool Stop = false; + Transform enemyGFX; + Animator animator; + Collider2D groundCheckCollider; - public Collider2D groundCheckCollider; - - // Health + [Header("Health")] public int health = 100; public GameObject deathEffect; - bool TimeStopHearting = false; + + bool StopHurting = false; bool StopAttack = false; + bool Stop = false; bool InNear = false; + int currentWaypoint = 0; void Start() { seeker = GetComponent(); rb = GetComponent(); + groundCheckCollider = GetComponent(); animator = GetComponentInChildren(); player = GameObject.FindGameObjectWithTag("Player").transform; + enemyGFX = GetComponentInChildren().transform; + InvokeRepeating("UpdatePath", 0f, .5f); } @@ -47,7 +48,7 @@ public class Claudia : MonoBehaviour { if (seeker.IsDone()) { - seeker.StartPath(rb.position, target.position, OnPathComplete); + seeker.StartPath(rb.position, player.position, OnPathComplete); } } @@ -62,7 +63,13 @@ public class Claudia : MonoBehaviour void FixedUpdate() { - if (Pause.IsPause == false && Stop == false && InNear == true && StopAttack == false) + if (Pause.IsPause) + { + return; + } + + float distance = Vector2.Distance(transform.position, player.position); + if (Stop == false && InNear == true && StopAttack == false) { if (path == null) { @@ -78,7 +85,7 @@ public class Claudia : MonoBehaviour rb.AddForce(force); - float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]); + distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]); if (distance < nextWaypointDistance) { @@ -95,8 +102,9 @@ public class Claudia : MonoBehaviour enemyGFX.transform.localScale = new Vector3(1f, 1f, 1f); } } + distance = Vector2.Distance(transform.position, player.position); - if (distance < range && Pause.IsPause == false) + if (distance < range) { InNear = true; distance = Vector2.Distance(transform.position, player.position); @@ -105,12 +113,19 @@ public class Claudia : MonoBehaviour Destroy(gameObject); } } + + // check if the enemy is not on the ground + if (!IsOnGround()) + { + // destroy the enemy game object + DestroyEnemy(); + } } void OnCollisionEnter2D(Collision2D collision) { - if (collision.gameObject.tag == "Player" && StopAttack == false) + if (collision.gameObject.tag == "Player" && !StopAttack) { StartCoroutine(Attack()); } @@ -118,7 +133,8 @@ public class Claudia : MonoBehaviour void OnTriggerEnter2D(Collider2D collider) { - if (collider.gameObject.tag == "Bullet" && distance < range && TimeStopHearting == false) + float distance = Vector2.Distance(transform.position, player.position); + if (collider.gameObject.tag == "Bullet" && distance < range && !StopHurting) { StartCoroutine(BulletAttacked()); TakeDamage(50); @@ -127,59 +143,40 @@ public class Claudia : MonoBehaviour public void TakeDamage(int damage) { - if (MainMenu.ExitLevel == false) + if (!MainMenu.ExitLevel) { health -= damage; if (health <= 0) { - Die(); + DestroyEnemy(); } } } - void Die () - { - Instantiate(deathEffect, transform.position, Quaternion.identity); - Destroy(gameObject); - } - IEnumerator Attack() { StopAttack = true; - //Stop = true; + animator.SetBool("Attack", true); Enemy.TookDamage = true; yield return new WaitForSeconds(2.0f); animator.SetBool("Attack", false); + StopAttack = false; - //Stop = false; } IEnumerator BulletAttacked() { Stop = true; animator.SetBool("Damage", true); - TimeStopHearting = true; + StopHurting = true; yield return new WaitForSeconds(2.0f); - TimeStopHearting = false; + StopHurting = false; animator.SetBool("Damage", false); Stop = false; } - void Update() - { - if (Pause.IsPause == false) - { - // check if the enemy is not on the ground - if (!IsOnGround()) - { - // destroy the enemy game object - DestroyEnemy(); - } - } - } - bool IsOnGround() { // perform an overlap check with the ground check collider diff --git a/Assets/game/enemy/Justin/Justin.prefab b/Assets/game/enemy/Justin/Justin.prefab index c1b946e..63928aa 100644 --- a/Assets/game/enemy/Justin/Justin.prefab +++ b/Assets/game/enemy/Justin/Justin.prefab @@ -225,4 +225,5 @@ MonoBehaviour: range: 10 speed: 5 groundCheckCollider: {fileID: 5214278713793197941} + collisionWithPlayerCollider: {fileID: 2445183410012274874} deathEffect: {fileID: 1755349173569476, guid: 3c45864be6a059a4a82a25e285298338, type: 3} diff --git a/Assets/game/enemy/Justin/Scripts/Justin.cs b/Assets/game/enemy/Justin/Scripts/Justin.cs index 3803622..9fe41a0 100644 --- a/Assets/game/enemy/Justin/Scripts/Justin.cs +++ b/Assets/game/enemy/Justin/Scripts/Justin.cs @@ -4,15 +4,20 @@ using UnityEngine; public class Justin : MonoBehaviour { + [Header("Speed & Range")] public float range = 10f; public float speed = 5f; + [Header("Colliders")] + public Collider2D groundCheckCollider; + public Collider2D collisionWithPlayerCollider; // USE TOP COLLIDER!!! (For some reason it works better this way) + + [Header("Death Effect Prefab")] + public GameObject deathEffect; + + private Animator playerAnimator; private Transform player; private Rigidbody2D rb; - public Collider2D groundCheckCollider; - - public GameObject deathEffect; // the death effect prefab - private Animator playerAnimator; void Start() { @@ -44,6 +49,12 @@ public class Justin : MonoBehaviour { DestroyEnemy(); } + + if (CollidesWithPlayer()) + { + Enemy.TookDamage = true; + DestroyEnemy(); + } } } @@ -51,21 +62,10 @@ public class Justin : MonoBehaviour { if (collision.gameObject.tag == "Player") { - // The enemy has collided with the player - if (collision.relativeVelocity.y < 0 && transform.position.y < collision.transform.position.y) - { - // The player is colliding with the top of the enemy from above - // Destroy the enemy game object - PlayerMovement.jump = true; - playerAnimator.SetBool("IsJumping", true); - DestroyEnemy(); - } - else - { - // The player is colliding with the side or bottom of the enemy, or the player is not colliding with the top of the enemy from above - // Destroy the player game object - Enemy.TookDamage = true; - } + //If player jumps on top + PlayerMovement.jump = true; + playerAnimator.SetBool("IsJumping", true); + DestroyEnemy(); } } @@ -86,6 +86,23 @@ public class Justin : MonoBehaviour return false; } + bool CollidesWithPlayer() + { + // perform an overlap check with the isJumpedOn collider + Collider2D[] colliders = Physics2D.OverlapBoxAll(collisionWithPlayerCollider.bounds.center, collisionWithPlayerCollider.bounds.size, 0f); + + // iterate through the colliders and check if any of them are considered ground + foreach (Collider2D collider in colliders) + { + if (collider.gameObject.CompareTag("Player")) + { + return true; + } + } + + return false; + } + void DestroyEnemy() { diff --git a/Assets/game/enemy/Zoltan/Scripts/Zoltan.cs b/Assets/game/enemy/Zoltan/Scripts/Zoltan.cs index 109caf4..ecc49fe 100644 --- a/Assets/game/enemy/Zoltan/Scripts/Zoltan.cs +++ b/Assets/game/enemy/Zoltan/Scripts/Zoltan.cs @@ -4,15 +4,20 @@ using UnityEngine; public class Zoltan : MonoBehaviour { + [Header("Speed & Range")] public float range = 10f; public float speed = 5f; + [Header("Colliders")] + public Collider2D groundCheckCollider; + public Collider2D collisionWithPlayerCollider; // USE TOP COLLIDER!!! (For some reason it works better this way) + + [Header("Death Effect Prefab")] + public GameObject deathEffect; + + private Animator playerAnimator; private Transform player; private Rigidbody2D rb; - public Collider2D groundCheckCollider; - - public GameObject deathEffect; // the death effect prefab - private Animator playerAnimator; void Start() { @@ -44,6 +49,12 @@ public class Zoltan : MonoBehaviour { DestroyEnemy(); } + + if (CollidesWithPlayer()) + { + Enemy.TookDamage = true; + DestroyEnemy(); + } } } @@ -51,21 +62,10 @@ public class Zoltan : MonoBehaviour { if (collision.gameObject.tag == "Player") { - // The enemy has collided with the player - if (collision.relativeVelocity.y < 0 && transform.position.y < collision.transform.position.y) - { - // The player is colliding with the top of the enemy from above - // Destroy the enemy game object - PlayerMovement.jump = true; - playerAnimator.SetBool("IsJumping", true); - DestroyEnemy(); - } - else - { - // The player is colliding with the side or bottom of the enemy, or the player is not colliding with the top of the enemy from above - // damage the player - Enemy.TookDamage = true; - } + //If player jumps on top + PlayerMovement.jump = true; + playerAnimator.SetBool("IsJumping", true); + DestroyEnemy(); } } @@ -86,6 +86,23 @@ public class Zoltan : MonoBehaviour return false; } + bool CollidesWithPlayer() + { + // perform an overlap check with the isJumpedOn collider + Collider2D[] colliders = Physics2D.OverlapBoxAll(collisionWithPlayerCollider.bounds.center, collisionWithPlayerCollider.bounds.size, 0f); + + // iterate through the colliders and check if any of them are considered ground + foreach (Collider2D collider in colliders) + { + if (collider.gameObject.CompareTag("Player")) + { + return true; + } + } + + return false; + } + void DestroyEnemy() { diff --git a/Assets/game/enemy/Zoltan/Zoltan.prefab b/Assets/game/enemy/Zoltan/Zoltan.prefab index 6ba704f..ed4ce00 100644 --- a/Assets/game/enemy/Zoltan/Zoltan.prefab +++ b/Assets/game/enemy/Zoltan/Zoltan.prefab @@ -225,4 +225,5 @@ MonoBehaviour: range: 10 speed: 5 groundCheckCollider: {fileID: 4120503342754605680} + collisionWithPlayerCollider: {fileID: 5808843570160950719} deathEffect: {fileID: 1755349173569476, guid: 3c45864be6a059a4a82a25e285298338, type: 3}