• 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 Shmulzious · Jul 22, 2013 at 06:34 PM · clones

Handeling Collision between clones and objects with same name/tag

Hey Guys,

Not so new coder, but new to Unity - been searching for someone with a similar issue and no luck. Was hoping someone here could help me out.

I've got a prefab with a tag called "Asteroid", and it has a mesh collider - it works fine with objects with different tags, but when I try to handle collisions of an asteroid with itself (instantiated clones and other smaller asteroids with the same tag).

Here is basically the code I use just to get a "hit" print it is a componenet on the asteroid object.

 function OnTriggerEnter(other: Collider){
 if(other.tag.Equals("Asteroid")){
 print("hit");
 }
 }

Thanks for taking the time, Sam.

Comment
Add comment · Show 2
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 Muuskii · Jul 22, 2013 at 06:35 PM 1
Share

What is happening when two asteroids collide? What do you want to happen? What have you tried to resolve this issue?

avatar image Shmulzious · Jul 22, 2013 at 06:40 PM 0
Share

They go right through each other, I want them to reverse direction as soon as they hit eachother, I've tried to identify them by name (Asteroid(Clone)) to see if it registered but nothing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Muuskii · Jul 22, 2013 at 06:45 PM

If two objects of the same type are not registering collisions with eachother first thing I would check is if their Collision layers are setup to interact with themselves.

I'm getting the feeling you have a custom movement script? Or are you using rigidbody physics?

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 Shmulzious · Jul 22, 2013 at 06:54 PM 0
Share

Actually, didn't even remember the collision layers thing, so thanks for that I'll keep it in $$anonymous$$d for the future :)

Though I haven't implemented this, so all the objects are on the same layer.

I am using AddForce(), and some other scripts activated through different objects. Here they are (I know some are bad practice and I should make more variables public ect. Though this is a learning project I am intending to share so sorry for the mess :))

 //This one is from my main camera script
 void FixedUpdate()
     {
         //Apply force to Asteroids
         GameObject[] currentAsteroids = new GameObject[GameObject.FindGameObjectsWithTag("Asteroid").Length];
         currentAsteroids = GameObject.FindGameObjectsWithTag("Asteroid");
         if (currentAsteroids.Length > 0){
             for(int i = 0; i < currentAsteroids.Length; i++){
                 if(currentAsteroids[i].name.Equals("Asteroid(Clone)")){
                 currentAsteroids[i].rigidbody.AddForce(Random.Range (-20,20),Random.Range (-20,20), 25);
                 }
                 if(currentAsteroids[i].name.Equals("midAsteriodBit(Clone)")){
                 currentAsteroids[i].rigidbody.AddForce(Random.insideUnitCircle * 100);
                 }
                 if(currentAsteroids[i].name.Equals("smallAsteroidBit(Clone)")){
                 currentAsteroids[i].rigidbody.AddForce(Random.insideUnitCircle * 200);
                 }
             }
         }
 }
 
 //This one is from my player child "$$anonymous$$agnet" script
 
 var magneticStrength = 50;
 
 function OnTriggerStay(other : Collider)
     {
         var direction = (transform.position-other.transform.position);
 
         if(direction.sqr$$anonymous$$agnitude <= magneticStrength*magneticStrength) // if we're inside the range of the magnetic
         {
             other.rigidbody.AddForce(direction.normalized * magneticStrength);
         }
     }
 
 //This one is from my Asteroid script
 
     //Asteroids are stopped at the borders of the screen and pushed away from them with rigidbody.Addforce
     //Top Border
     if(transform.position.y >= 28.5){
         transform.position = new Vector3(transform.position.x,28.5f,transform.position.z);
         rigidbody.AddForce(0,-50,25);
     }
     //Bottom border
     if(transform.position.y <= -28.5){
         transform.position = new Vector3(transform.position.x,-28.5f,transform.position.z);
         rigidbody.AddForce(0,50,25);
     }
     //Left border
     if(transform.position.x >= 38.5){
         transform.position = new Vector3(38.5f,transform.position.y,transform.position.z);
         rigidbody.AddForce(-50,0,25);
     }
     //Right border
     if(transform.position.x <= -38.5){
         transform.position = new Vector3(-38.5f,transform.position.y,transform.position.z);
         rigidbody.AddForce(50,0,25);
     }
     
     transform.Rotate(2,2,2);
 

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

16 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 avatar image avatar image

Related Questions

Instantiate Prefab Array with ID 2 Answers

How do I create a spring between two specific clones? 0 Answers

Tetris, Tetrominoes Clones Spawn Same Time 0 Answers

Deleting Bullets after a few seconds 2 Answers

Assign same value to multiple instances of the same prefab 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