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 Steviebops · Aug 13, 2013 at 10:06 PM · collisiontriggereventsphere

How do I trigger an event (health loss) only once?

I have a one-on-one fighting style game.

I have sphere colliders for the weapons and various body parts. Using this code, I can trigger the 'hit' animation, if (weaponCollider.bounds.Intersects(bodCollider.bounds)) { if (animation.IsPlaying("AnnaHammerJab") && animation["AnnaHammerJab"].time > .5f) { Debug.Log("-1 health");

             }
         }

And this is fine, my problem is when I look at the Debug.log, it fires repeatedly, which is what I assume health code would do.

If the weapon sphere skims the hit sphere, it only logs for a frame or two, at close range, it fires for longer. This would mean that more health would be lost, the longer the two spheres intersect.

What I would like is for the health to be subtracted only once (in this case, the Debug.log is the stand in for health code.

Is there a way to trigger it only once per intersection?

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
1

Answer by RyanZimmerman87 · Aug 14, 2013 at 12:00 AM

Depending on how you programmed your game you may want to use Animation Events. A good tutorial can be seen for that here:

https://www.youtube.com/watch?v=QNdGBBsC9_I

This would allow you to call functions based on the animation timing of your characters and could eliminate the possibility of multiple hits.

If your game is a 2D street fighter type game this may be preferable. So when they hit the right spot of their attack animation it would call the animation event and then at that point your function could check if the collision occurs at that exact moment to determine what happens.

However if it is more of a 3d fighting game where you should be able to hit them at multiple points during an attack swing I would recommend some kind of control bool like the other user suggested.

You must be careful with these though because they do have potential for more bugs than an Animation Event. For example if the player wiggles back and forth very quickly while they are attacking they may be able to exploit the collision detection and still execute multiple hits in one strike by entering and leaving the collision zone. However you could also eliminate that behavior with additional variable checks similar to what the other user posted above.

So in summarry:

If you want super easy animations to deal with that call a function once per animation I would use an Animation Event.

If you want really realistic combat that can detect all kinds of different moments of combat whether the strike if just starting or nearly complete you should use something like the failSafeVariables listed above, and be prepared to potentially use multiple conditions to control it depending on how your game works.

Comment
Add comment · Show 1 · 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 Steviebops · Aug 14, 2013 at 01:35 AM 0
Share

Lot's of good answers here, Ill try them all, and report what works.

Ive tried a bool before, but it was being set true during the entire collision, therefore draining health the entire time. The approach above looks different to what I did though.

Ill look into Animation Event, the game is a 3d style (Soul Calibur)game, however.

avatar image
0

Answer by RomanorumLegio · Aug 13, 2013 at 11:49 PM

I don't know what the proper name of it would be, but I personally would use what I would call a "fail safe" variable.

Try something like:

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
     private bool failSafeVar = true;
 
     void Update () {
         if (weaponCollider.bounds.Intersects(bodCollider.bounds)) {
             if (failSafeVar == true){
                 if (animation.IsPlaying("AnnaHammerJab") && animation["AnnaHammerJab"].time > .5f) { 
                     failSafeVar = false;
                     Debug.Log("-1 health");
                 }
             }
         }
         else {
             failSafeVar = true;
         }
     }
 }
 


The idea is that while the two colliers were in contact, the failSafeVar would only be true to allow damage once otherwise it would be false.

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 Steviebops · Aug 14, 2013 at 02:24 AM 0
Share

RomanorumLegio - You answer is working for me :)

I had a slight problem with the sphere not following the weapon mesh it's attached to, so I attached it to the bone the weapon is attached to as well.

Thank you all for your help :D

avatar image RomanorumLegio · Aug 14, 2013 at 02:39 AM 0
Share

Happy to be of service.

avatar image
0

Answer by Elroyman · Aug 14, 2013 at 12:00 AM

The other option is the OnCollisionEnter method that is built into the system...just toss it down in your script and it only gets called the one time that the two intersect.

 void OnCollisionEnter(Collider col)
 {
      if (col.gameObject.tag = "[ENTER TAG HERE]")
           Debug.Log("-1 Health");
 
 }

Obviously the conditional statement is up to you, but I would consider applying a tag to each of the objects (projectiles) you'll be shooting. If this method is invoked on any of the scripts attached to the target, it result in only one subtraction each time/ You could also trigger the animation in the same statement.

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

18 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

Related Questions

OnTriggerEnter is not running! Need help with collision. 2 Answers

Plane flyby triggering/help 1 Answer

Sphere and Cube collision won't work,Sphere vs Cube collision won't work 1 Answer

create event on collision 2 Answers

Collision not triggered by box collider 2 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