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 /
  • Help Room /
avatar image
0
Question by zachmallet · Oct 17, 2016 at 11:01 PM · 2dcollisioncollider2ddamagehealth

Inconsistent 2d collider hit registration

I'm working on a simple melee system and I'm getting very inconsistant hit registration. here is my Attack Trigger code

 using UnityEngine;
 using System.Collections;
 
 public class AttackTrigger : MonoBehaviour {
     public int dmg;
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "Enemy")
         {
             col.gameObject.SendMessageUpwards("Damage", dmg);
             Debug.Log("Swing");
         }
     }
 }

Enemy code:

 public class Enemy : MonoBehaviour {
 
     public int health;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (health <= 0)
         {
             Destroy(gameObject);
         }
     }
 
     public void Damage(int damage)
     {
         health -= damage;
         //gameObject.GetComponent<Animation>().Play("name");
         Debug.Log("Hit");
     }
 }

Player attack code:

 public class PlayerAttack : MonoBehaviour {
 
     Rigidbody2D rbody;
     Animator anim;
     public Collider2D hitbox;
     private bool Attacking = false;
     // Use this for initialization
     void Start () {
         rbody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
         hitbox.enabled = false;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0))
         {
             if (!Attacking)
             {
                 hitbox.enabled = true;
                 anim.SetTrigger("Attack");
                 Attacking = true;
             }
             else
             {
                 Attacking = false;
                 hitbox.enabled = false;
             }
         }
         else
         {
             Attacking = false;
             hitbox.enabled = false;
         }
     }
 }

Here's a visual gif of whats happening https://i.gyazo.com/28f584c42cb0c33c447d6e6de50d813c.gif

Thanks for any help.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zachmallet · Oct 18, 2016 at 04:53 PM

I changed my code to call the animation on click then enable the hitbox from the animation itself. Solving all the issues I was having.

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
avatar image
0

Answer by jahester · Oct 18, 2016 at 02:03 AM

I had a sort of a similar problem recently and it turned out to be due to two different scripts looking for a collider hit on the same gameobject. I my case it was an icon to toggle a GUI menu open or closed. When the wrong script intercepted the hit first, the menu would stay in its current state and it appeared nothing was happening. I added a static boolean to keep track of which of the two colliders should be active based on the current state of the menu.

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
avatar image
0

Answer by DevilZ1976 · Oct 18, 2016 at 04:27 PM

First question why don't you use GetComponent instead of SendMessageUpwards on your collider gameobject?

And why do control attack on mousedown, don't you need any cooldown for next attack?

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

126 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

Related Questions

Circle Collider2D Stuck ob Box Collider2D at the corner :( 0 Answers

Health variable not changing 1 Answer

Best practice where to store Projectile Damage amounts? 1 Answer

Can't get health/damage to work 0 Answers

Trying to collide in grid like movement 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