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 luther1321 · Aug 28, 2014 at 06:26 AM · collisiondestroyrandomontriggerenternumber

Edit - Code isolation using random ID numbers C#

 void OnTriggerEnter(Collider otherParticle){
     //makes sure the code only happens when you collide with another prefab sphere.
     if (otherParticle.gameObject.tag == "Proton") {
         //add the mass of this sphere to the other sphere
         otherParticle.rigidbody.mass = rigidbody.mass + otherParticle.rigidbody.mass;
         //destroy this sphere leaving only the other sphere containing the combined mass
         Destroy (gameObject);
     }
 }

I have these spheres. These spheres are programmed to gravitate towards one another. On the collision I want to add the mass of them together and the result be a single sphere. I'm still also trying to figure out the radius increasing upon combining them. Help with that, too, would be sweet!

To elaborate, this script is applied to every sphere I have in the scene. My issue is with the fact that upon collision both spheres are destroyed. I understand the code is being ran on both objects because the sphere collider trigger is the same radius on both of them so both spheres issue the code upon collision. I have to tried reduce the size of the sphere collider set as a trigger and the regular sphere collider, so that they are contained within the mesh of the sphere. Using the transport controls for the colliders, I set them apart from each other within the sphere mesh. I was hoping that if the collider on one entered only the collider trigger on the other then only one of them would be destroyed, but it seems that OnTriggerEnter issues two different ways. When this game objects collider enters another gameobjects collider trigger and also when this gameobjects collider trigger is entered into by another gameobjects collider.

I need help isolating this code to only one of the spheres! PLZ :)

Comment
Add comment · Show 3
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 revolute · Aug 28, 2014 at 07:43 AM 0
Share

Why don't you make a manager script where it would decide who gets to be destroyed as well as alter its size and mass?

avatar image khaled235711 · Aug 28, 2014 at 08:14 AM 0
Share

Try Destroy(otherParticle.gameObject) as it will destroy the other and it wouldn't be able to destroy this I mean the faster will remain the script who executes first will win

avatar image luther1321 · Aug 28, 2014 at 08:31 AM 0
Share

I have tried otherParticle.gameobject and the problem persists because the otherParticle is still also trying to delete this particle that's trying to delete the other particle lol... How would I go about crafting a manager script? I was thinking of something along the lines of a separate gameobject that handled that, but I don't know how to reference a collision between two objects from another gameobject/script. Do You?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kiwasi · Aug 28, 2014 at 09:22 AM

Manager pseudo code as follows

 // On the particle
 ParticleManager.DestroyMe (gameObject, otherGameObject);
 
 // The Manager
 public void DestroyMe (GameObject go1, GameObject go2){
     if (go1 && go2){
         Destroy (go1);
     }
 }

An alternate solution without the manager would be to use unique IDs. I've used a random number, but you could also use a static int to generate the ID. Pseudo code:

 public float ID = Random.Range(0f,10f);
 
 // Inside the collision code
 if(ID > other.ID){
     Destroy(gameObject);
 }

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 luther1321 · Aug 29, 2014 at 12:08 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Protons : $$anonymous$$onoBehaviour {
 
     private float ID;
     private Protons otherProton;
 
     void Update(){
         ID = Random.Range(0f,10f);
     }
     void On$$anonymous$$ouseDown(){
         Destroy(gameObject);
     }
 
     void OnTriggerEnter(Collider otherParticle){
         if(otherParticle.gameObject.tag == "Proton" && gameObject.tag == "Proton"){    
             otherProton = otherParticle.GetComponent<Protons> ();
             if(ID > otherProton.ID){
                 rigidbody.mass=rigidbody.mass+otherParticle.rigidbody.mass;
                 Destroy(otherParticle.gameObject);
             }
         }
     }
 }

Thanks so much for the help! The ID's work like a charm!

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

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

Related Questions

Randomly keeping paintball intact on collision 1 Answer

Creating a Asteroid health script 5 Answers

Destroy bullet on random collision 2 Answers

Collision, but then only affect ONE of the gameObjects? 2 Answers

2 objects collide, need to destroy one 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