Added health bar to slimes and improved attack logic

This commit is contained in:
finnchen123
2025-12-10 16:39:24 +01:00
parent abc11650ee
commit f74ec7e84d
7 changed files with 407 additions and 117 deletions

View File

@@ -20,6 +20,7 @@ namespace Assets.Scripts.InteractableObjects
Stopwatch jumpTimer;
bool followsPlayer;
bool isAttacking;
bool isAttackOnCooldown;
Stopwatch attackTimer;
// Start is called before the first frame update
@@ -30,41 +31,57 @@ namespace Assets.Scripts.InteractableObjects
jumpTimer.Start();
followsPlayer = false;
isAttacking = false;
isAttackOnCooldown = false;
attackTimer = new Stopwatch();
//Nothing
}
// Update is called once per frame
void Update()
{
if (gameObject.activeSelf)
Jump();
if (isAttacking)
{
if (!isJumping)
Attack();
}
}
void Attack()
{
if (!isAttackOnCooldown)
{
attackTimer.Start();
isAttackOnCooldown = true;
}
if (attackTimer.ElapsedMilliseconds >= 2000)
{
attackTimer.Reset();
isAttackOnCooldown = false;
}
}
void Jump()
{
if (!isJumping)
{
jumpTimer.Start();
isJumping = true;
Vector3 jumpVector;
if (followsPlayer)
{
jumpTimer.Start();
isJumping = true;
Vector3 jumpVector;
if (followsPlayer)
{
jumpVector = GameObject.Find("Player").transform.position - gameObject.transform.position;
jumpVector *= 0.25f * Mathf.Log10(jumpVector.magnitude);
jumpVector.y = 5;
}
else
{
jumpVector = new Vector3(rand.Next(-5, 5), 10, rand.Next(-5, 5));
}
gameObject.GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.Impulse);
jumpVector = GameObject.Find("Player").transform.position - gameObject.transform.position;
jumpVector *= 0.25f * Mathf.Log10(jumpVector.magnitude);
jumpVector.y = 5;
}
if (jumpTimer.ElapsedMilliseconds >= 5000)
else
{
jumpTimer.Reset();
isJumping = false;
}
if (gameObject.transform.position.x > 1000 || gameObject.transform.position.x > 5000 || gameObject.transform.position.x > 5000)
{
UnityEngine.Debug.Log("BUG!");
jumpVector = new Vector3(rand.Next(-5, 5), 10, rand.Next(-5, 5));
}
gameObject.GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.Impulse);
}
if (jumpTimer.ElapsedMilliseconds >= 5000)
{
jumpTimer.Reset();
isJumping = false;
}
}
@@ -76,7 +93,7 @@ namespace Assets.Scripts.InteractableObjects
}
}
public void handleAttack()
public void HandleAttack()
{
isAttacking = true;
//Create attack logic here
@@ -86,10 +103,11 @@ namespace Assets.Scripts.InteractableObjects
{
followsPlayer = isFollowing;
isAttacking = false;
//Stop attacks
attackTimer.Reset();
isAttackOnCooldown = false;
}
public void handleDetection()
public void HandleDetection()
{
followsPlayer = true;
}

View File

@@ -242,10 +242,10 @@ namespace Assets.Scripts.Player
switch (col.name)
{
case "AttackRange":
enemy.handleAttack();
enemy.HandleAttack();
break;
case "DetectionRange":
enemy.handleDetection();
enemy.HandleDetection();
break;
case "FollowRange":
//Do nothing, as it needs to be detected first