Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Valorous_Entity · Mar 18, 2016 at 12:29 PM · polygon collider 2dhitbox

Polygon Collider 2D Problem.

Okay so basically my main character uses a sword to attack and I have made a polygon collider around it to use for collisions against enemies. Everything is set up, but when I attack, multiple enemies take damage at the same time instead of just one. It's very confusing and I haven't been able to solve this problem. Here is the polygon collider script

 using UnityEngine;
 using System.Collections;
 
     public class HeroHitBoxManager : MonoBehaviour {
     
         // Set these in the editor
         public PolygonCollider2D frame2;
         public PolygonCollider2D frame3;
         public PolygonCollider2D frame4;
     
         // Used for organization
         private PolygonCollider2D[] colliders;
     
         // Collider on this game object
         private PolygonCollider2D localCollider;
     
         // We say box, but we're still using polygons.
         public enum hitBoxes
         {
             frame2Box,
             clear // special case to remove all boxes
         }
     
         void Start()
         {
             // Set up an array so our script can more easily set up the hit boxes
             colliders = new PolygonCollider2D[]{frame2};
     
             // Create a polygon collider
             localCollider = gameObject.AddComponent<PolygonCollider2D>();
             localCollider.isTrigger = true; // Set as a trigger so it doesn't collide with our environment
             localCollider.pathCount = 0; // Clear auto-generated polygons
         }
     
         void OnTriggerEnter2D(Collider2D col)
         {
             Debug.Log("Collider hit something!");
         }
     
         public void setHitBox(hitBoxes val)
         {
             if(val != hitBoxes.clear)
             {
                 localCollider.SetPath(0, colliders[(int)val].GetPath(0));
                 return;
             }
             localCollider.pathCount = 0;
         }
     }

And here is the health script.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class HealthScript : MonoBehaviour {
 
 
 
     private GameMaster gm; 
 
     public bool isEnemy = true;
     public float max_Health = 100f; 
     public float cur_Health = 0f; 
     public GameObject healthBar; 
 
     void Start()
     {
         gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> (); 
         cur_Health = max_Health;
         InvokeRepeating ("decreaseHealth", 1f, 1f);
     }
 
     void Update()
     {
         
     }
 
     void decreaseHealth()
     {
         
         //uncomment if you want to test health bar. 
 
         
         float calc_Health = cur_Health / max_Health;
         SetHealthBar (calc_Health);
      
     }
 
     public void SetHealthBar(float myHealth)
     {
         healthBar.transform.localScale = new Vector3 (myHealth, healthBar.transform.localScale.y, healthBar.transform.localScale.z);
     }
 
     // Update is called once per frame
     void OnTriggerEnter2D (Collider2D collider) 
     {
 
         HeroHitBoxManager hit = collider.gameObject.GetComponent<HeroHitBoxManager> ();
         hit = null; 
         if (hit != null && gameObject.tag == "Enemy") 
         {
             cur_Health = cur_Health - 5; 
         }
             if (cur_Health <= 0 && gameObject.tag == "Enemy") {
                 Destroy (gameObject);
                 gm.points -= 1;
                 
                 if (gm.points == 0){
                     SceneManager.LoadScene("WinScreen");
                 }
                     
             }
             if(cur_Health <= 0 && gameObject.tag == "Player"){
             Application.LoadLevel("YouLose"); 
             }
     }
 
 void OnCollisionEnter2D (Collision2D col)
 {
     //Check collision name
     Debug.Log("collision name = " + GetComponent<Collider2D>().gameObject.name);
         if(col.gameObject.tag == "Enemy")
     {        
             cur_Health = cur_Health - 5;     
     }
 
     }
 }
 
       
 


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 shieldgenerator7 · Mar 28, 2018 at 09:07 PM 0
Share

It's because all enemies that enter the collider will get hit. You have to set a bool variable to keep track of if the sword has hit something yet, and if it has, then don't process anymore attacks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by starrtennis · Aug 07, 2019 at 01:00 AM

This sounds like a health script issue, not a polygon collider issue. Is your health a static variable by mistake?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't delete random vertices from a PolygonCollider 1 Answer

Particle Emitter crashing! 1 Answer

Knockback and Launch Angle 1 Answer

Child not working with triggerEnter 2 Answers

How do I make Hitboxes in a 2d fighting game using Unity with 2d ToolKit? 0 Answers


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