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 Blink · Sep 21, 2012 at 09:03 PM · guiplayerenemyhealth

Deduct health on collision

The last question went a bit off topic. This script I am writing is supposed to decrease the players health if anything with a tag of Enemy collides with it. It doesn't seem to do this and Im not sure what Im supposed to do, also if I could have some more help with having a timer so it only takes of damage every second or so, that would be great, thanks.

 var fullHealth : int = 100;
 var curHealth : int = 100;
 
 function OnCollisionEnter(collision: Collision) {
 
 if(collision.gameObject.tag == "Enemy");
   curHealth -= 10;
   print ("hit");
 }
 
 function Update () {
 
 if(curHealth >= fullHealth){
     curHealth = fullHealth;
 }
 
   if(curHealth <= 0){
     curHealth = 0;
     Debug.Log("You are Dead");
 }
 }
 
 function OnGUI() {
     GUI.Label (Rect (25, 40, 100, 20), "Health = "+curHealth);
 }
Comment
Add comment · Show 1
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 AlucardJay · Sep 21, 2012 at 10:13 PM 0
Share

originally from : http://answers.unity3d.com/questions/320641/taking-a-hit.html

2 Replies

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

Answer by GoCatGoGamesLLC · Sep 22, 2012 at 02:20 PM

It seems, at least from a logic perspective, that you are misusing "Full" and "Current" health levels. The "Full Health" represents the highest health level, a static number that should not be changed. "Current Health" is your bread and butter.

I'll make a leap and assume that your checking Current against Full in the Update to assure that Current never exceeds the maximum. I would change the second IF to reflect:

 if ( curHealth <= 0 ) {
     curHealth = 0;
     Debug.Log ( "You are Dead" );
 }

Similar issue in your OnCollisionEnter FUNCTION:

 if ( collision.gameObject.tag == "Enemy" );
   curHealth -= 10;
   print ( "hit" );
 }

Concentrate on using Current Health as your "working" health level and leave the Full Health variable to simply describe the maximum.

Hope that helps.

Edited to Add: You'll need to change your GUI as well (Full to Current).

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 Blink · Sep 22, 2012 at 05:14 PM 0
Share

Ive updated my code in the question but it doesn't change anything.

avatar image
1

Answer by skalev · Sep 21, 2012 at 09:43 PM

This is bit confusing. First, is curHealth supposed to be the current health, then it is the one you should be editing, not the full health. also, I don't understand the purpose of checking if curHealth is => fullHealth. But that is aside the point. In any case, it should work fine with this code, what I would guess is the issue is that OnCollisionEnter is never called, so there is no hit. have you established that the function is called? (that is, do you see your hit debug message?)

Comment
Add comment · Show 5 · 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 Blink · Sep 22, 2012 at 01:59 PM 0
Share

The console never says that it gets hit which must mean its never called, how can I fix this?

avatar image fafase · Sep 22, 2012 at 02:01 PM 0
Share

Does any of your object has a rigidbody? I think you need at least one of them to have one.

avatar image Blink · Sep 22, 2012 at 05:13 PM 0
Share

Yeah, my enemy has a rigidbody, but adding one to my player changes nothing so it does not.

avatar image fafase · Sep 22, 2012 at 05:15 PM 0
Share

What kind of collider are you using? If mesh collider tick convex or better use a primitive collider like a box.

avatar image Blink · Sep 28, 2012 at 04:40 PM 0
Share

I figured it out, is was because of my colliders.

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

12 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

Related Questions

Taking a hit 3 Answers

Restart with GUIText 2 Answers

Adding a counter? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Need Help About Health,Damage 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