• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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? 5 Answers

collider ignore other collider at a small angle 1 Answer

Rigid Bodies & Colliders 3 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges