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 /
This question was closed Aug 14, 2013 at 11:43 AM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DeadKenny · Aug 14, 2013 at 11:02 AM · c#oncollisionenteroncollisionhealth

Damage by bullets issue. Solved.

Ok next problem.

I have two separate codes here that don't work exactly as wanted... again. One is the health of the AI and the other is the bullets being fired at this AI.

More specific is that the health is not reduced according to the damage that I have set in the bullet. It instead reduces the thing to 0 instantly when a bullet hits it.

Each bullet is supposed to do 10 damage and the health of the AI is 100. Even if I set the health 1000 it goes right down to zero and destroys the AI from one bullet.

Codes:

 AI
 
 public int maxHealth = 100;
 public int curHealth = 100;
 
 
 
 public void HealthAdjust(int damage){
 
     curHealth =- damage;
     
     if(curHealth < 0)
        curHealth = 0;
 
     if(curHealth > maxHealth)
        curHealth = maxHealth;
     //stop devision from zero.
     if(maxHealth < 1)
         maxHealth = 1;
     
 
    if(curHealth == 0){
         
      Destroy(gameobject);
      
 
      }
 
 
 }
 
 
 //This is the bullet
 
 void OnCollisionEnter(Collision col){
 
    public int damage = 10;
    
    
    //warhead is the effect.
    
    GameObject.Instantiate(warhead, transform.position, Quaternion.identity);
 
   col.gameObject.BroadCastMessage("HealthAdjust", damage, SendMessageOptions.DontRequireReceiver);
 
 
 Destroy(gameObject);
    
 
     
 
 
 }
 
 
 
 

So again, the bullet is not reducing the health by 10 as supposed to, instead it reduces all of the health no matter the actual size.

What am I doing wrong?

Comment
Add comment · Show 2
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 reppiz01 · Aug 14, 2013 at 11:10 AM 0
Share

is this code in one scriptfile?

avatar image DeadKenny · Aug 14, 2013 at 11:41 AM 0
Share

No... Woops I just typed in the same code here. They are separate.

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by meat5000 · Aug 14, 2013 at 11:07 AM

Try -= instead of =-

  curHealth -= damage;  //Take damage from curHealth and store result in curHealth;
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 DeadKenny · Aug 14, 2013 at 11:42 AM 0
Share

DOH!

Thanks man. It works.

avatar image meat5000 ♦ · Aug 14, 2013 at 11:43 AM 0
Share

:P Go on, click the thumbs up :D Thanks :)

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

make OnCollisionEnter2D ignore collisons of child objects 0 Answers

(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers

Health UI Display 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