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
0
Question by UKT2 · Sep 06, 2017 at 10:29 PM · collider2dontriggerenter2dhitbox

Collider 2d not working

Basically I made a box collider gameobject which is being enable and disabled by the animation. The problem is that a have a enemy which walks towards player when it is in range it plays attack animation. Because of the animation the hitbox is enabled first and disabled again and it applies damage to the player. But the problem is if the player doesn't do anything and just stands there the animation keeps playing the hitbox is enabled and disable again but it doesn't apply the damage. It is like the onTriggerEnter Function is not working when the player position isn't changed at all , even if the hitbox is disabled n then enabled again.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jiak1 · Sep 06, 2017 at 10:32 PM

Try using

 void OnCollisionStay(Collision collision)
     {
         
     }

It gets called every frame for each collider that is touching it. Although it would be easier from your enemy script to call say an event on the player that damages it for example: In your enemy have:

     void AttackPlayer()
     {
         //Play my animation
         GameObject player = GameObject.Find("Player");
         player.GetComponent<HealthScript>().takeDamage(20);
     }

In your player have

     void takeDamage(int amount)
     {
         health -= amount;
     }
Comment
Add comment · Show 2 · 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 UKT2 · Sep 06, 2017 at 10:58 PM 0
Share

I tried collision stay/ ontriggerStay but the problem is it calls every frame and i have hitboxes that last for some time.

avatar image jiak1 UKT2 · Sep 06, 2017 at 11:19 PM 0
Share

Try something like this but just check if can attack is true and each time you attack make sure to StartCoroutine(WaitForNextAttack()); which will wait in seconds the attackspeed and reset can attack back to true.

     public bool canAttack = true;
     public float attackSpeed = 5.0f;
     IEnumerator WaitForNextAttack()
     {
         yield return new WaitForSeconds(attackSpeed);
         canAttack = true;
     }

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

69 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

Related Questions

OnTriggerEnter2D not working 2 Answers

Trigger only one collider? 1 Answer

How to get the position of a Tile my Player collided with using OnTriggerEnter2D? 0 Answers

OnTriggerEnter2D is called 2 more times in Melee combat system 1 Answer

OnTriggerEnter2D fires multiple times when Animator component is on 3 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