• 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
Question by Garthazin · Aug 24, 2012 at 01:37 PM · collisionobjectspecific

How can I do a collision check with a specific Object?

Hi, I'm really new to Unity scripting and am having trouble getting a certain collision script working.

My player object is a tank with 2 Forms of Health, Hull and Plating. It can shoot either a shell or bullets. Another gameobject I have is a Stationary Turret that fires a bullet.

On the tank; How Can I check that it is the Turret Bullet that has collided with it?

On the turret; How can I check not only whether it is the Shell or the Bullet that it collides with? I need to check which of the two, as they subtract differing amounts of the turret's health.

If it helps at all, here's what I have so far...

 //in the script attached to the tank
 
 void OnTriggerEnter (Collider other)
     {
         if (other.name == "Bullet")
         {
          if (OuterPlating > 0)
             {
                 
             OuterPlating -= 20;    
             Destroy(other);    
                 
             }
         if (OuterPlating <= 0)
             {            
             HullIntegrity -= 20;    
             Destroy(other);
             }
         
         }
         
         
     }
 

and...

 //in the script for the turret
 
 void OnTriggerEnter(Collider other)
     {
         
         if (other.name == "TankShell")    
         {
             TurretHealth -= 50;
             Destroy(other);
         }
         else if (other.name == "TankBullet")
         {
             TurretHealth -= 5;    
             Destroy(other);
         }
     }
Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by BLarentis · Aug 24, 2012 at 02:45 PM

Hello, what you did it's right. For the OnTriggerEnter to work, one of the objects that are colliding with the other needs a rigid body. And probably your tank and your turret already have one. And your bullet and shell need to have the collider marked as trigger.

If at every shot you instantiate a new bullet, what isn't working is that you are checking for the name of the object, like "TankShell", "TankBullet" and "Bullet". When you instantiate a new object he will receive the prefab name plus (Clone) at the end. What you can do its to check fot the tag of the game object. Create tags with those names, select the respective tag to those objects and check for this:

 other.tag == "Bullet"

and so on. Take care!

Comment

People who like this

0 Show 0 · 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

Answer by Mander · Aug 24, 2012 at 02:52 PM

maybe ur colliders arent triggers... u might need to change ur scripts. instead of OnTriggerEnter to OnCollisionEnter(Collision other), or check the trigger checkbox. ur logic should work

Comment

People who like this

0 Show 0 · 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

Answer by Arshia001 · Aug 24, 2012 at 03:02 PM

This kind of thing can quickly get out of hand if your code gets big. Imagine a game in which there are 5 different turrets, and the player(or tank) has 20 different weapons. Each of the turrets and the player will need to check for each of the different types of bullets/shells/grenades/RPG rockets/etc. Rather than checking tags, I'd suggest you create a script called Bullet, and apply it to the projectile prefabs. In that script, you can define how much damage that specific projectile does, thus eliminating the if/else blocks, like this:


class Bullet
{
   public int damage;
}

You'd also have a better way to check if the object is actually a bullet of not, by checking if it has a Bullet script attached, like this:

void OnTriggerEnter(Collider other) { Bullet b; if ((b = other.transform.GetComponent<Bullet>()) != null) health /* or TurretHealth or Shield or whatever */ -= b.damage; }

Comment

People who like this

0 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

on collision, show/hide other model scripting ? 1 Answer

the sound will not play 2 Answers

how to check if one object is colliding with another 1 Answer

Collision of a moving object with a standard 3rd person controller 1 Answer

Stop object collision 3 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