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 msyoung2012 · Jun 19, 2014 at 12:49 AM · c#collisioninstantiateprocedural-generationdestory

Destroying an instantiated object colliding with another instantiated object

Broad question: How do you destroy an instantiated object if it's instantiated in a position that makes it collide with another objected generated by the same script?

Specific situation: So I'm procedurally generating a forest with 2 kinds of trees (flufftrees and short flufftrees - there will be more types later if I can figure this out!). Here is the C# code that generates them:

 using UnityEngine;
 using System.Collections;
 
 
 
 public class TreeSeeder : MonoBehaviour {
 
     public int flufftreecount;
     public int shortfluffcount;
     public int randposmin;
     public int randposmax;
     
     public Rigidbody flufftree;
     public Rigidbody flufftree_short;
 
     // Use this for initialization
     void Start () {
 
         //Making flufftrees
         for (int i=0; i < flufftreecount; i++){
             
             
             //random coordinates generated
             int randpos = 3 * Random.Range(randposmin, randposmax);
             int randpos2 = 3 * Random.Range(randposmin, randposmax);
             int randrot = Random.Range(1,359);
             
             //parse into position
             Vector3 flufftreepos = new Vector3(randpos, 0, randpos2);
 
             //instantiate object at random coordinates
             Rigidbody flufftree_child  = Instantiate(flufftree, flufftreepos, Quaternion.identity) as Rigidbody;
             flufftree_child.transform.rotation = Quaternion.Euler(0,randrot,0);
 
             flufftree_child.tag = "Nature";
 
             yield return null;
 
             // Removing if intersecting other Nature objects 
             void OnCollisionEnter(Collision col)
             {
 
                 if (col.gameObject.tag == "Nature" )
                 {
                     Destroy(col.gameObject);
                 }
             }
 
             
             
         }
 
 
         //making shortfluffs
         for (int j=0; j < shortfluffcount; j++){
             
             
             //random coordinates generated
             int randpos = 3 * Random.Range(randposmin, randposmax);
             int randpos2 = 3 * Random.Range(randposmin, randposmax);
             int randrot = Random.Range(0,360);
             
             //parse into position
             Vector3 shortfluffpos = new Vector3(randpos, 0, randpos2);
 
             //instantiate object at random coordinates
             Rigidbody shortfluff_child  = Instantiate(flufftree_short, shortfluffpos, Quaternion.identity) as Rigidbody;
             shortfluff_child.transform.rotation = Quaternion.Euler(0,randrot,0);
 
             shortfluff_child.tag = "Nature";
 
             yield return null;
 
             // Removing if intersecting other Nature objects 
             void OnCollisionEnter(Collision col)
             {

                 if (col.gameObject.tag == "Nature")
                 {
                     Destroy(col.gameObject);
                 }
             }
 
         }
 
     
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 }

My problem: I need to destroy any trees which are generated in space already occupied by another tree. I thought that sections of code like this would do that, but they seem to have no impact on the situation:

 // Removing if intersecting other Nature objects 
                 void OnCollisionEnter(Collision col)
                 {
 
                     if (col.gameObject.tag == "Nature")
                     {
                         Destroy(col.gameObject);
                     }

I also included yields before these sections to allow a frame to pass before the script checks for collisions with other "Nature" tagged trees (all trees will have this tag). But alas, no luck.

I think I have the right idea here... what am I doing wrong?

Comment
Add comment · Show 4
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 voncarp · Jun 19, 2014 at 02:59 AM 0
Share

Did you try debugging the col.gameobject to see if its actually having a collision? Your collision script should be on the tree. You can also try the OnTriggerEnter() approach. I've had more success with triggers ins$$anonymous$$d of collisions.

avatar image voncarp · Jun 19, 2014 at 03:07 AM 0
Share

A more viable solution would be to shoot a ray at the location to check if another tree is there. And if it is, then don't spawn in another tree.

avatar image Mayank Ghanshala · Jun 19, 2014 at 11:02 AM 0
Share

Is this possible to put one method inside another method???? Or I am missing some coding basis????????

avatar image msyoung2012 · Jun 19, 2014 at 11:28 AM 0
Share

Do you mean check "is kinematic" on the empty game object this script is attached to or assign the property "is kinematic" to the instantiated object in the script? How would you do that in the script? I put the collision method in Start because I just want to check when the tree is instantiated (once).

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ProtoTerminator · Jun 19, 2014 at 03:06 AM

Unity for some reason doesn't detect collisions with objects that aren't moving. To get around this you need to put a rigidbody on your objects and check Is Kinematic. That should fix that problem. I'm not sure how expensive rigidbodies will be, but you should be able to just remove the rigidbody via script after instantiation if it becomes a problem.

Also, you'll need to be careful with this approach, as it's very easy to destroy both trees (the collision will be called on both of them).

Comment
Add comment · Show 5 · 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 ProtoTerminator · Jun 19, 2014 at 03:10 AM 0
Share

Also, looking at your code more, I'm not sure what putting a collision method inside the start method does. I usually put the collision methods on a separate script that I attach directly to the gameobject prefab.

avatar image meat5000 ♦ · Jun 19, 2014 at 07:10 PM 0
Share

Use can use OverLapSphere to check for other objects within the bounds of another.

There are other ways too, but I forget those for the moment.

avatar image msyoung2012 · Jun 19, 2014 at 10:00 PM 0
Share

Adding the kinematic rigidbody component to the prefab has no effect... same problem as before :/

avatar image ProtoTerminator · Jun 19, 2014 at 11:52 PM 0
Share

you put the rigid body on both trees?

avatar image msyoung2012 · Jun 20, 2014 at 12:21 AM 0
Share

Yes I made all prefabs kinematic rigidbodies

avatar image
0

Answer by Ricewind1 · Jun 19, 2014 at 11:13 AM

You could create a mini collision script and add it to both trees when they are instantiated. This way you'll be sure to only destroy the one triggering the collision event. You can even let this code generate automatically so that it's re-usable by other instances (namely the if(tag=tree) part.

Comment
Add comment · Show 3 · 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 msyoung2012 · Jun 19, 2014 at 09:57 PM 0
Share

@ricewind1 this was what I was trying to do but it doesn't work. Adding it to the prefabs doesn't work either.

avatar image Ricewind1 · Jun 20, 2014 at 12:19 AM 0
Share

What is it that "doesn't work" about this?

avatar image msyoung2012 · Jun 20, 2014 at 12:22 AM 0
Share

It doesn't remove intersecting trees. Applying this script to the prefabs didn't destroy them either.

avatar image
0

Answer by DarKTower · Jun 20, 2014 at 12:31 AM

Here's a quick solution I thought up in my head, but have no idea if it would work or not:

  1. Make sure both tree Prefabs' Rigidbodies are NOT Kinematic.

  2. Instantiate the trees

  3. Instead of checking for OnCollisionEnter (Which, to my knowledge, is only called when a Collider enters another Collider through force, not by Instantiating).

  4. Check for OnCollisionStay instead.

  5. If there is a OnCollisionStay detected, destroy one of the objects (Whichever you want to destroy)

  6. If there is NOT an OnCollisionStay detection (By either a WaitForSeconds yield or another method of your choosing), set the Rigidbody of the object to be Kinematic.

  7. Go on your merry way.

OnCollisionStay only works when one is not Kinematic, thus the reason for setting it to Kinematic afterwards. OnCollisionStay is also called even when an object is Instantiated inside another object (At least I believe it is), which OnCollisionEnter is not.

Good luck!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues with instantiated prefabs 1 Answer

Need help on spawning of objects from random brick in a brick breaker game 0 Answers

How to make objects spawn at the same time without colliding ? 1 Answer

How can I check if an instantiated object collides with another instantiated object? 1 Answer

Removing an object if it spawns in a building 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