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 potu1304 · Jan 14, 2013 at 07:27 PM · c#fpsbulletdamagehealth

Health and Damage [C#]

Hello!

I have to scripts:

One bullet script where I have the damage and a damage script where the health of the enemy is set. When the bullet hits the enemy there should be damage. But I have a mistake in my scripts, but where is it?

 Bullet Script:

 using UnityEngine;

 using System.Collections;

 

 public class Bullet : MonoBehaviour {

     

     

     

 public Rigidbody bullet;

 public float Force = 2000.0f;

 public AudioSource Shoot;

 public Transform spawnpoint;

 public float damage = 10.0f;

 

 

     void OnCollisionEnter(Collision col)

     {

         col.gameObject.BroadcastMessage("ApplyDamage", 5.0F);

         

     }

 

 

     // Use this for initialization

     void Start () {

     

     }

     

     // Update is called once per frame

     void Update () {

         

 //if(canControl == true){

 

 if(Input.GetButtonUp("Fire1")){

 Rigidbody shot;

 shot = Instantiate(bullet, spawnpoint.position, transform.rotation)as Rigidbody;

 shot.rigidbody.AddForce(transform.forward * Force);

 audio.Play();

     

     }

 //}

     }

 }



 Damage Script:
 using UnityEngine;
 using System.Collections;
 
 public class Damage : MonoBehaviour {
 
     public float Health = 100.0f;
     public Rigidbody deadReplacement;
 //    public Rigidbody dead;
 
     
     public void ApplyDamage (float damage)
     {
         Health =- damage;
         
         if (Health <= 0.0)
         return;
     }
     
 
 
     
     
     // Use this for initialization
     void Start () {
         
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public void Detonatin(){
         
         Destroy(gameObject);
         
         // If we have a dead barrel then replace ourselves with it!
     if (deadReplacement) {
         Rigidbody dead = (Rigidbody)Instantiate(deadReplacement, transform.position, transform.rotation);
 
         // For better effect we assign the same velocity to the exploded barrel
         dead.rigidbody.velocity = rigidbody.velocity;
         dead.angularVelocity = rigidbody.angularVelocity;
     }
         
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Chronos-L · Jan 15, 2013 at 02:07 AM

Your bullet script is not attached to the bullet your are firing. In

 shot = Instantiate(bullet, spawnpoint.position, transform.rotation)as Rigidbody;

You make a bullet and apply force to it. But the OnCollisionEnter() is not working as the shot you made doesn't have the bullet script

You should move your

 if( canControl == true ) {
    ...
 }

to another Firing script. What you will have then is

a) Damage Script (Attached to an enemy)

b) Bullet Script (Attached to the Rigidbody bullet)

 public class Bullet : MonoBehaviour {
    public float damage = 10.0f;
 
    void OnCollisionEnter(Collision col)
    {
       col.gameObject.BroadcastMessage("ApplyDamage", damage); 
    }
 }

c) Firing Script (Attached to an empty object or your player)

 public class Firing : MonoBehavior {
    public Bullet bullet;
    public float Force = 2000.0f;
    public AudioSource Shoot;
    public Transform spawnpoint;
 
    void Update () {
       //Nothing's changed from your previous code
       if(canControl == true){
       ...
       }
    }
 }


Friendly Tips: Check the setup of your bullet and enemies ( Collider & Rigidbody ), then use a Debug.Log("Collision with " + col.gameObject.name) to check whether a collision has occured.

Lastly, this is not related to the question but

 public void ApplyDamage (float damage)
 {
    Health =- damage;
 
    if (Health <= 0.0)   // <---What does this one do? Are you planning to call Detonatin() here?
       return;
 }
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 potu1304 · Jan 15, 2013 at 02:29 PM 0
Share

Ok thanks for the help. And yes I am planning to call a Detonation where a Rigidbody should be called when the enemy is dead.

avatar image
0

Answer by potu1304 · Jan 19, 2013 at 01:10 PM

I tried to add a rigidbody when the enemy is dead, so that it is not moving anymore. But it does not really works.

 using UnityEngine;
 using System.Collections;
 
 public class Damage : MonoBehaviour {
 
     public float Health = 100.0f;
     public Rigidbody deadReplacement;
 //    public Rigidbody dead;
     public bool deadTrue = false;
     public GameObject gameObject;
 
     
     public void ApplyDamage (float damage)
     {
         Health =- damage;
         
         if (Health <= 0.0){
         deadTrue = true;
         }
     }
     
 //    void OnTriggerEnter(Collider bullet)
 //    {
 //        GetComponent<Bullet>();
 //        Health =- Bullet.damage;
 //        
 //        
 //    }
 
     
     
     // Use this for initialization
     void Start () {
         
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public void Detonatin(){
         
         Destroy(gameObject);
         
         // If we have a dead barrel then replace ourselves with it!
     if (deadTrue = true){
         Destroy(gameObject);
         Rigidbody dead = (Rigidbody)Instantiate(deadReplacement, transform.position, transform.rotation);
 
         // For better effect we assign the same velocity to the exploded barrel
         dead.rigidbody.velocity = rigidbody.velocity;
         dead.angularVelocity = rigidbody.angularVelocity;
             
     }
         
     }
 }
 
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 Chronos-L · Jan 20, 2013 at 01:57 AM 0
Share
  1. Where do you called your Detonatin()?

  2. I am not able to see the reason behind the variable bool deadTrue

  3. Is dead.rigidbody.velocity a correct syntax? I question this because you declared dead as a type Rigidbody.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

bullet damage problem 1 Answer

Take health from enemy 3 Answers

damage system 0 Answers

Health and damage reciever 1 Answer

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