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
-1
Question by bbbbb123 · Jan 26, 2013 at 08:49 PM · c#getcomponentdamagehealth

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great.

/PlayerHealth/

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
     public int maxHealth = 100;
     public int curHealth = 100;
     
     public float healthBarLength;
 
 
     void Start () {
     healthBarLength = Screen.width / 2;
     }
     
 
     void Update () {
     AddjustCurrentHealth(0);
         
     }
     
     
     void OnGUI(){
     GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);    
     }
     
    
     public void AddjustCurrentHealth(int damage) {
       curHealth -= damage;    
         
         if(curHealth < 0)
             curHealth = 0;
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         
         if(maxHealth < 1)
             maxHealth = 1;
         if(curHealth == 0)
         {
             Destroy(gameObject);
         }
         healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
 }





/bullet/

using UnityEngine; using System.Collections;

public class bullet : MonoBehaviour { public int Damage = 10; public float Speed = 1500f; public float lifetime = 5f;

 void Start()
 {
             
     rigidbody.AddRelativeForce(new Vector3(0,0,Speed), ForceMode.Impulse);
     Destroy(gameObject, lifetime);
 }
 void Update()
 {
     
 }
 void OnTriggerEnter (Collider col)
 {  
     print("y1");
     if(col.tag == "Player")
     {
     col.gameObject.SendMessageUpwards("AddjustCurrentHealth", Damage, SendMessageOptions.DontRequireReceiver);
     print("y2");
     }    
 } 

}

Comment
Add comment · Show 4
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 Dave-Carlile · Jan 26, 2013 at 08:49 PM 0
Share

What problem are you having?

avatar image bbbbb123 · Jan 27, 2013 at 02:23 AM 0
Share

just their no script error but when the physical bullet collides nothing happens to the player's health, the player and the bullet are trigger.

avatar image SrBilyon · Jan 27, 2013 at 02:42 AM 0
Share

Try not making the player a trigger. What type of collider are using on the player object?

avatar image bbbbb123 · Feb 08, 2013 at 04:44 AM 0
Share

it work when my gun is touching, but else nothing happens when they hit

2 Replies

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

Answer by bbbbb123 · May 02, 2013 at 01:59 AM

hi everyone I figured out the problem, I put my bullet speed to fast for the game engine to compute it. the script above works perfectly fine in C#. thank you to everyone that help and those who even attempts.

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 iwaldrop · Feb 08, 2013 at 05:27 AM

You are passing a negative value as damage and then subtracting it on the other side as well. Pass a positive damage value to your health script.

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 bbbbb123 · Feb 27, 2013 at 05:24 AM 0
Share

thx for that but it not even colliding. I will change it here too

avatar image Lairinus · May 02, 2013 at 01:26 AM 0
Share

How fast is your bullet moving? If it's anywhere close to the speed of a real bullet, you need to change it to a Raycast and calculate damage that way.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

bullet damage problem 1 Answer

Distribute terrain in zones 3 Answers

C# add helth over time 1 Answer

My Photon health script doesnt work 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