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 Different · Aug 22, 2012 at 01:10 PM · c#damagehealth

Decrase value on collision C#

I got a Health Script from BurgZergArcade. And I need something that decrase the curHealth when the enemy (with tag "Enemy") will touch me, for example -10. Also, some delay between decrasing curHealth would be nice c:

Health Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
 public int maxHealth = 100; //maximum  HP
 public int curHealth = 100; //current HP
 
 public float healthBarLength;
 
 // Use this for initialization
 void Start () {
 healthBarLength = Screen.width / 2;
 }
 
 // Update is called once per frame
 void Update () {
 AddjustCurrentHealth (0);
 }
 void OnGUI(){
 GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth); 
 }
 
 public void AddjustCurrentHealth(int adj) {
 curHealth += adj;
 
 if(curHealth < 0)
 curHealth = 0;
 
 if (curHealth > maxHealth)
 maxHealth = 1;
 
 healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
 }
 }


Comment
Add comment · Show 7
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 Khada · Aug 22, 2012 at 01:35 PM 0
Share

Ins$$anonymous$$d of asking for someone else to do it for you, it's important that you try to learn how to do these things for yourself. It's only going to make it harder and slower in the long run if you don't. There are TONES of answers for how to do something on a collision event - use the search function to find one and go play with it.

The point of this site is to help you learn, not do it for you (though that certainly happens). At least have a go at it first, then come to us with your specific problem when you get stuck.

avatar image Different · Aug 22, 2012 at 01:43 PM 0
Share

I know about it, man! I'm actually trying to make it work for about 2 hours. Sadly, I'm newbie and this is hard. $$anonymous$$y spcific problem? It's not working ("Ba Dum Pss" in the background).

avatar image Khada · Aug 22, 2012 at 01:52 PM 0
Share

Ok, so put up what you've got so it can be corrected etc. What have you got working so far? What are you stuck on?

Is this your first project? Have you done tutorials first?

avatar image Different · Aug 22, 2012 at 01:58 PM 0
Share

Yes and Yes.

I was trying:

 void OnTriggerEnter(Collider col){
    //Things happens here
 }

And:

void OnCollisionEnter(Collision collision) { //Things happens here }

But it's still not working properly. I'm better in JavaScript.

avatar image Khada · Aug 22, 2012 at 02:03 PM 0
Share

You have one of your colliders set to trigger and a rigid body on your object(s)?

Show more comments

2 Replies

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

Answer by Kryptos · Aug 22, 2012 at 02:08 PM

Collision detection is done (in script) using events sent by Unity engine. For example OnCollisionEnter is sent to the GameObject with a Collider component attached or to the parent Rigidbody when using compound colliders.

Once you know that, you can try a dummy solution like:

 void OnCollisionEnter(Collision collision)
 {
     curHealth -= 10;
 }



Of course, you may now want to check the kind of object that you collided with. In order to do that, you can check the name or the tag of the collided object.

 String tag = collision.gameObject.tag;
 if (tag == "Enemy")



And finally, to make sure that you decrease the value nex timte only after a certain amount of time, you need to use the so-called Time class.

Each time you change the value, you can store the time that event occured:

 lastChangeTime = Time.time;

Next attempt to change the value, you check the time elapsed since the previous change:

 if (Time.time - lastChangeTime > aCertainAmountOfTime)
 {
     // change the value
     AdjustHealth(-10);
     // save the 'last change time'
     lastChangeTime = Time.time;
 }

With all that, I'm sure you can write your own solution.

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 Different · Aug 22, 2012 at 02:12 PM 0
Share

Thanks a lot! I'll try to mix this to one working script :)

avatar image
0

Answer by Stardog · Aug 22, 2012 at 02:08 PM

You are looking for OnCollisionEnter

     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Enemy")
         {
             AdjustCurrentHealth(-10);
         }
     }

Also, you will need a rigidbody attached (it's a physics collision). And remove the AdjustCurrentHealth from Update.

For a time delay:

 IEnumerator AdjustCurrentHealth(int adj)
 {
     yield return new WaitForSeconds(5);
     ...

Then StartCoroutine(AdjustCurrentHealth(-10)); where it was before.

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 Kryptos · Aug 22, 2012 at 02:17 PM 0
Share

StartCoroutine won't solve the issue here. You're just delaying the change but you don't prevent the coroutine from being called more than once during this delay.

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

10 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

Related Questions

My Photon health script doesnt work 2 Answers

(C#) Enemy health and take damage from bullets 1 Answer

Photon Combat script does not work 0 Answers

Take health from enemy 3 Answers

Instantiate on value change 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