Search Unity

C# Is there a way to check if 2 objects are within a certain radius of each other?

Discussion in 'Scripting' started by From-Soy-Sauce, Jul 24, 2014.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hi I wanted to know if there was a way to check if 2 objects are within a certain distance of each other And if they are I want one of them to do something.

    Let me explain the scenario I am going for:

    I have 2 kinds of objects, balloon types, and spike types. And I'd like to make it so that every balloon checks the distance it is from every other balloon. And if the distance is less than 3 I want it to print "good". and every balloon also should check the distance from all spikes, and if the distance is less than 3, it should print "bad."

    By the way, I have the "types" of objects defined by their tag: "balloon" tag and "spike" tag.
     
    Last edited: Jul 25, 2014
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Vector3.Distance(gameObject.transform.position, otherGameObject.transform.position) ?
     
    GorillaHead7 likes this.
  3. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Ahh that sounds about right, I'll try it out.
     
    Last edited: Jul 25, 2014
  4. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Sorry I cannot really figure it out after messing with it for about 40 minutes.

    Let me explain the scenario I am going for:

    I have 2 kinds of objects, balloon types, and spike types. And I'd like to make it so that every balloon checks the distance it is from every other balloon. And if the distance is less than 3 I want it to print "good". and every balloon also should check the distance from all spikes, and if the distance is less than 3, it should print "bad."

    By the way, I have the "types" of objects defined by their tag: "balloon" tag and "spike" tag.
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Two ways to do this, one you setup an array of the objects you need to test against and loop through all the objects, the problem is that if you are testing against every other object you need two nested loops and the number of tests goes up very fast e.g. 3 objects = 4 tests, 10 objects = 45 tests.

    It would be easier to set up trigger collision spheres on the objects of the size you need then use the OnTriggerEnter or OnTriggerStay methods to detect a balloon getting to close to another or a spike. You will have to set up the layer of the trigger so it does not detect the ground or none important objects.

    But try both on your target platform to see how well they perform.
     
  6. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Well, I mean, I'm asking how to do this stuff, because I don't know how to go about trying to code any described methods yet. Though I would prefer things be done using as JUST code and as little inspector as possible.

    Let me put it this way: Is there a way to something for all objects of a certain tag? Here is just an example of what kind of effect I am looking for using pseudocode:


    balloonPosition=position

    With all objects with tag "spike"
    {
    warnDistance=distance between myPosition and baloonPosition
    if warnDistance< 3 Print("bad")
    if warnDistance< 1 Destroy(balloon)
    }

    I firstly want to know if a system like this even exists in C# unity.
     
    Last edited: Jul 25, 2014
  7. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
  8. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    I've been trying to get this to work since reading your post and all I get are red errors.

    I've tried copying and pasting the example, and changing some names around, but in either case I just get red errors.
     
  9. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK post your code and the errors. I am willing to help you work it out but I draw the line at writing the code for you.
     
  10. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Ok, well Like I said I just copy and pasted the first code (renaming things appropriatly) on the link you gave me because I wanted to try and understand how it worked, and to my surprise, it simply didn't work.
    I have always been able to learn most effectively when I'm allowed to break down a functional script to figure out how it works.... That said, I have a really really really hard time trying to break down a script that doesn't work because I am then forced to try and imagine it working, how it's supposed to be like to make it work, and what would make it work that way.
     
  11. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Any idea what that error even means? Because I sure don't have any.
     
  12. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    So I've been continuing trying to figure out this, one step at a time. And I winded up being able to get something that doesn't have errors but it's also not doing what I want it to.

    Can you explain how I can get this to print the transform.position.y of the "hah" tagged objects instead of the "circ" object?

    Code (CSharp):
    1. using UnityEngine;using System.Collections;public class circ : MonoBehaviour
    2.     {
    3.     void Start() {}
    4.     void Update()
    5.         {
    6.         var fish = GameObject.FindGameObjectsWithTag("hah");
    7.         foreach (Object respawn in fish)
    8.             {
    9.             print (transform.position.y);
    10.             }
    11.         }
    12.     }
     
  13. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    print(respawn.transform.position.y)
     
  14. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    That didn't work. Even with the ";" added at the end, i get this error:

    Assets/circ.cs(9,39): error CS1061: Type `UnityEngine.Object' does not contain a definition for `transform' and no extension method `transform' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)
     
  15. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Sorry, try print(respawn.gameObject.transform.position.y);

    I'm not at my PC so I can't actually test it.
     
  16. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Also why are you using object?
    Why not do
    foreach(GameObject respawn in fish) {

    }
     
  17. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Thank you for trying.

    Though that gives me this error:

    Assets/circ.cs(9,31): error CS1061: Type `UnityEngine.Object' does not contain a definition for `gameObject' and no extension method `gameObject' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)
     
  18. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Ahh, ok thanks! now it works!

    BTW, I was using objects because that what they used in the example that Arowx posted.