Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shazilshahzad111 · May 22, 2021 at 10:16 PM · 3dcombatfightingame

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;
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image undevable · May 22, 2021 at 10:27 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Replacing OverlapSphere with colliders (Detecting weapon collision with enemies - SOLVED) 1 Answer

3D Combo system? 1 Answer

how can i manually update unity web player ? 0 Answers

Raycast shooting in the middle of the screen 0 Answers

How to add to a 3D object's length through code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges