- Home /
When I want to kill an enemy with a strong attack, the enemy just gets stuck. How do I fix this?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class attack : MonoBehaviour { public Animator attackAnim; public Transform attackPoint; public float attackRange; public LayerMask enemyLayers;
public int normalDamage;
public int heavyDamage;
public float attackRate =2f;
float nextAttackTime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject theDagger = GameObject.Find("Dagger");
pickup pdagger = theDagger.GetComponent<pickup>();
GameObject theMace = GameObject.Find("Mace");
macepickup pmace = theMace.GetComponent<macepickup>();
GameObject theSword = GameObject.Find("Sword");
swordpickup psword = theSword.GetComponent<swordpickup>();
if(pdagger.daggerEquipped == true)
{
attackPoint = theDagger.transform.Find("ATTACKPOINT1");
normalDamage = 20;
heavyDamage = 30;
}
if(pmace.maceEquipped == true)
{
attackPoint = theMace.transform.Find("ATTACKPOINT2");
normalDamage = 45;
heavyDamage = 55;
}
if(psword.swordEquipped == true)
{
attackPoint = theSword.transform.Find("ATTACKPOINT3");
normalDamage = 15;
heavyDamage = 27;
}
if(Time.time >= nextAttackTime)
{
//normal attack
if(Input.GetKeyDown(KeyCode.Mouse0))
{
attackAnim.SetTrigger("isAttacking");
Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);
foreach(Collider enemy in hitEnemies)
{
enemy.GetComponent<normalEnemy>().TakeDamage(normalDamage);
}
nextAttackTime = Time.time + 1f/attackRate;
}
//strong attack
if(Input.GetKeyDown(KeyCode.Mouse1))
{
attackAnim.SetTrigger("isStrongAttacking");
Collider[] strongHit = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);
foreach(Collider strongHitThis in strongHit)
{
strongHitThis.GetComponent<normalEnemy>().TakeHeavyDamage(heavyDamage);
}
nextAttackTime = Time.time + 1f/attackRate;
}
}
}
void OnDrawGizmosSelected()
{
if(attackPoint == null)
return;
Gizmos.color = new Color(1,1,0,0.75f);
Gizmos.DrawSphere(attackPoint.position, attackRange);
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class normalEnemy : MonoBehaviour { public Animator animator; bool normalhit;
public int maxHealth = 100;
public int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int normalDamage)
{
animator.SetTrigger("normalHit");
currentHealth -= normalDamage;
}
public void TakeHeavyDamage(int heavyDamage)
{
animator.SetTrigger("heavyHit");
currentHealth -= heavyDamage;
}
// Update is called once per frame
void Update()
{
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
animator.SetBool("isDead", true);
GetComponent<Collider>().enabled = false;
this.enabled = false;
}
}
Just a suggestion, GameObject.Find
is very extensive and performance-heavy, so consider just making it a public variable and assign the value in the Inspector.
Answer by rh_galaxy · May 23, 2021 at 12:28 PM
You should think about what is causing your objects (colliders) to move into each other. That's the one thing that can make them stuck...
I think when you detect Physics.OverlapSphere(), it's already too late, and maybe you should set the Layer.Collission.Matrix (Edit->Projects Settings->Physics2D) so that your weapon and enemy don't collide...
But I'm not sure, maybe this gives you a hint in the right direction.