Refactored Claudia.cs & fixed a bug

This commit is contained in:
MarcUs7i 2024-10-09 21:02:22 +02:00
parent 8240db9777
commit 167a7a3e4c
6 changed files with 117 additions and 92 deletions

View file

@ -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

View file

@ -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<Seeker>();
rb = GetComponent<Rigidbody2D>();
groundCheckCollider = GetComponent<Collider2D>();
animator = GetComponentInChildren<Animator>();
player = GameObject.FindGameObjectWithTag("Player").transform;
enemyGFX = GetComponentInChildren<SpriteRenderer>().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

View file

@ -225,4 +225,5 @@ MonoBehaviour:
range: 10
speed: 5
groundCheckCollider: {fileID: 5214278713793197941}
collisionWithPlayerCollider: {fileID: 2445183410012274874}
deathEffect: {fileID: 1755349173569476, guid: 3c45864be6a059a4a82a25e285298338, type: 3}

View file

@ -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()
{

View file

@ -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()
{

View file

@ -225,4 +225,5 @@ MonoBehaviour:
range: 10
speed: 5
groundCheckCollider: {fileID: 4120503342754605680}
collisionWithPlayerCollider: {fileID: 5808843570160950719}
deathEffect: {fileID: 1755349173569476, guid: 3c45864be6a059a4a82a25e285298338, type: 3}