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 PenguinEmporium · Mar 01, 2015 at 12:09 AM · collisioncolliderdetectbodyrigid

Checking and destroying a rigidbody?

I'm trying to make a script that detects when a bullet hits an enemy, it will destroy the bullet, lower health, and if health is low enough, destroy the enemy. The code has been simplified to include on the health function.

 public Collision bulletcolide;  //Would you use this to designate the bullet collier?
     public int health = 3;
     
     void Update () {
             HealthFunc(bulletcolide);
             }
     
     void HealthFunc(Collision collision){ 
             if(collision.rigidbody)
             {
                 health = health -1;
                 Destroy (collision.gameObject);
             }
             if(health < 1)
             {
                 Destroy (gameObject);
             }
     
         }

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 PenguinEmporium · Feb 28, 2015 at 08:24 PM 0
Share

Checking for AND destroying the rigidbody.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by badatnames16 · Mar 01, 2015 at 01:03 AM

The way i would do it is if the script is attached to the bullet then you could put this code in

 void OnCollisionEnter(Collision collision)
     {
                 if (collision.gameObject.tag == "Enemy") 
         {
             Destroy(gameObject);
             (enemy script name) = collision.gameObject.GetComponent<(enemy script name)>();
             (enemy script name).enemyHealth -= 1;
         }
     }

that will destroy the bullet and take health from the enemy. you also need to attach a script to the enemy and create the health variable and in the bullet script you need to get the enemy script component.

To destroy the enemy when it dies you need to put if(enemyHealth <= 0) { Destroy(gameObject); }

in the update function of the enemy script

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 Loken_01 · Mar 01, 2015 at 01:10 AM

Hey PenquinEmporium!

The easiest method I see for this is to have a "Bullet" Component on the bullets that you shoot. Like so:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(SphereCollider))]
 public class Bullet : MonoBehaviour
 {
     void Update()
     {
         //Update your projectile/trajectory/etc
     }
 
     public void Impact()
     {
         //Do whatever you need to here
         Destroy (gameObject);
     }
 
     void OnCollisionEnter(Collision other)
     {
         if(other.gameObject.GetComponent<Player>())
         {
             Impact();
             other.gameObject.GetComponent<Player>().HealthFunc();
         }
     }
 }

Then from that in your "Player" class or whatever you have called it, you can use your health function like so:

 void HealthFunc()
 { 
     health--;
 
     if(health <= 0)
         Destroy (gameObject);        
 }


Also there are a bunch of other messages that Components offer!
Check here for all messages available on colliders

I hope this helps!

-Peace

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter2D is not detecting collisions. 2 Answers

Is it possible to check collision from another object? 6 Answers

collider ignore other collider at a small angle 1 Answer

This might be simple but i'm lost. 4 Answers

Colliding with static model 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