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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by AtomicMarine · Apr 27, 2012 at 10:27 AM · collisioninstantiatedestroyspawncollisiondetection

Two Objects Touching?

What is the best way to check if two objects, both with colliders, may have spawner on top of or next to each other, I tried using an algorithm where I find the short distance from one object to the closest tagged object however all the objects share the same tag so it always returns itself as the closest object?

Is there a way I can exclude the object the script is on from the array which is being created by finding tagged objects?

OR

Is there a way I can check collisions for objects that instantiate on top of or near to each other? My biggest problem with the collision functions in this case is that they don't actually collide (the objects) do they need a rigidbody to work properly? Ill post my script bellow nicely commented so people can understand.

 var thisG : GameObject;
 var enemies : GameObject[];
 var closestEnemy : GameObject;
 var dister : float = 1000;
 var dist : float ;
 var destroyer : float = 0.4;
 var timer : float = 0;

 function Start () {
 
 }
 
 function Update () {
         //The timer is to allow everything to spawn first
     timer += Time.deltaTime;
         // Creates an array of all the tagged objects
     enemies = GameObject.FindGameObjectsWithTag("arno");
     if(timer > 1){
     if (enemies.length > 0){
         var closestEnemy = enemies[0];
         var dister = Vector3.Distance(thisG.transform.position , enemies[0].transform.position);;    
         for (var m = 0; m < enemies.length;m++){
             var tempDist = Vector3.Distance(thisG.transform.position , enemies[m].transform.position);
             if (tempDist == 0){
         
         }else{
             if (tempDist < dister){
                 closestEnemy = enemies[m];    
                 //used to find the closest enemy
             }
         }
     }
     dist = Vector3.Distance(thisG.transform.position , closestEnemy.transform.position);
 }else{
     dist = 1000;    
 }
 if(dist == 0 ){
 
 }else{
     if (dist < destroyer){
             // if the enemy is to close to another enemy on spawn it destroys it
     Destroy(closestEnemy);
     Destroy(thisG);
     }
 }
     }
 }

 function OnCollisionEnter(hit : Collision) { // This whole function doesn't work i just kept it in the code to see if someone can edit it 
     
         if (hit.gameObject.tag == "arno"){
             Destroy(thisG);
     }
         
 }
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 Owen-Reynolds · Apr 27, 2012 at 02:19 PM 0
Share

I don't know about the rest, but you can check for "yourself" with something like if(gameObject==enemies[i]) continue; Continue is a trick that sort of puts the rest of the loop in an else -- jumps right to the top of the loop for i+1. It can save a lot of {}'s.

thisG is redundant -- are you using it to save lookup time? If so, set it in Start: thisG=gameObject;

avatar image AtomicMarine · Apr 27, 2012 at 02:27 PM 0
Share

Thanks for the "ThisG = gameObject" I hadn't refractored the code for elegance yet I am trying the continue thing, hopefully it works thanks :)

avatar image AtomicMarine · Apr 27, 2012 at 02:55 PM 0
Share

Problem solved!! thanks, the continue combined with some closer exa$$anonymous$$ation of the script helped me! if you post an answer ill be happy to select it, just be sure to include the fact that i was missing a second check for "dister" after I had changed the closest enemy :)

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to spawn an object continually while floating vertically like a bubble 1 Answer

Checking Collision on instantiated object 0 Answers

Detecting collision from Instantiated objects 1 Answer

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

How can I instantiate rigid bodies on top of each other without them exploding away? 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