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 sabin22 · Jan 15, 2014 at 02:34 PM · 2dcolliderhealth

Rigidbody Collider Hero health does not drain when touched by an enemy rigidbody collider.

This has been a hair puller for a few days now. I'm having trouble getting my hero's health to drain when an enemy touches him in my 2D platformer. Both are rigid bodies, both have colliders, and neither are triggers or kinematic. The enemy pushes the hero, so I know the colliders are working, but the hero's health does not decrease when touched. I can only assume it has something to do with my code, which I've tinkered with to no avail.

Any help would be much appreciated!

 var explosion : GameObject;
 var explosionSound : GameObject;
 var BlastenHealth : float;
 var damageRateTouch : float = 1f;
 var hitByEmemySFX : GameObject;
 private var RateCount : float = 0.0f;
 private var ResetCount : float = 0.0f;
 private var TriggerEffectsOnce : boolean = true;
 
 // Blasten explodes and respawns after 3 seconds
 function Update () {
     Debug.Log(BlastenHealth);
     if (BlastenHealth <= 0){
         if(TriggerEffectsOnce){
             Instantiate(explosion, transform.position, transform.rotation);
             Instantiate(explosionSound, transform.position, transform.rotation);
             TriggerEffectsOnce = false;
         }
         GetComponent(MeshRenderer).enabled = false;
         GetComponent(CharacterController).enabled = false;
         ResetCount += Time.deltaTime;
             if(ResetCount >= 3){
                 Application.LoadLevel("SandStage");
             }    
     }
     barDisplay = BlastenHealth * 0.01;
 }
 
 // Different types of damages to Blasten and their effects
 function OnCollisionEnter (col : Collider){
     if (col.gameObject.tag =="SmallEnemyCollision") {
         BlastenHealth = BlastenHealth - 10;
         Instantiate(hitByEmemySFX, transform.position, transform.rotation);
     }
     if (col.gameObject.tag =="BigEnemyCollision") {
         BlastenHealth = BlastenHealth - 15;
         Instantiate(hitByEmemySFX, transform.position, transform.rotation);
     }
     if (col.gameObject.tag =="Small Projectile") {
         BlastenHealth = BlastenHealth - 5;
         Instantiate(hitByEmemySFX, transform.position, transform.rotation);
     }
     
 }
 
 
 function OnCollisionStay (col : Collider) {
    if (col.gameObject.tag =="SmallEnemyCollision") {
            RateCount += Time.deltaTime;
            if (RateCount >= damageRateTouch){
                    BlastenHealth = BlastenHealth - 10;
                    Instantiate(hitByEmemySFX, transform.position, transform.rotation);
                 RateCount = 0.0f;
         }
    }
 }
Comment
Add comment · Show 3
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 Calum-McManus · Jan 15, 2014 at 02:35 PM 0
Share

Are you using the 2D feature in Unity 4.3?

avatar image robertbu · Jan 15, 2014 at 02:36 PM 0
Share

If this is one of the new 2D games with 2D colliders, then you need the 2D versions of the collision callbacks: OnCollisionEnter2D and OnCollisionStay2D.

avatar image Calum-McManus · Jan 15, 2014 at 02:37 PM 0
Share

^Exactly why I asked! I also believe it have the Collider2D function as well!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vitor_r · Jan 15, 2014 at 06:58 PM

You should use the Collision2D Events

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 sabin22 · Jan 16, 2014 at 03:54 PM

Thank you everyone for your responses! It turns out I just had to change "Collider" to "Collision" in the functions. I don't think I need to add "2D" because the colliders are 3D.

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

20 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

Related Questions

OnTriggerEnter and OnTriggerExit both trigger twice 0 Answers

C# Invalid Points Assigned to 2D Edge Collider 1 Answer

2D Function analyzing if there is a Gameobject where you click ? 4 Answers

Unity2D - Box Collider 2D problem 0 Answers

Do 2d Colliders cause problems when inside nested objects in Unity? 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