Search Unity

How to check the distance between two game objects?

Discussion in 'Scripting' started by W4RH4WK117, Apr 23, 2012.

  1. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    Hey guys,

    So here is my problem, basically I'm trying to create a script that checks if the distance between gameobject A and gameobject B is too far then gameobject A cannot to move to gameobject B and must take a shorter path. I'm not exactly sure on whats the best solution whether I use raycasts to determine the raycast length but they are not rigid bodies. Is there some sort of transform that can check the distance say in a if statement or something?

    Thanks for your help.
     
  2. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    var distance = Vector3.Distance(transformA.position, transformB.position);


    if (distance > 3)
    {
    // too far etc.
    }
     
  3. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    Doesn't seem to work, did you want me to post the code?
     
  4. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    ya post it
     
  5. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    Code (csharp):
    1. var waypoint: GameObject;
    2. var ShuttleSelection: GameObject;
    3. static var shuttleMove = false;
    4. static var travelSelection = false;
    5. var distance = Vector3.Distance(ShuttleSelection.position, waypoint.position);
    6.  
    7. function Start () {
    8.  
    9.  
    10.  
    11. }
    12.  
    13.  
    14.  
    15. function Update () {
    16.  
    17. waypoint = waypointSelection.selectedWaypoint;
    18. ShuttleSelection = ShuttleController.SelectedShuttle;
    19.  
    20. }
    21.  
    22. function OnGUI() {
    23.  
    24. if (ShuttleController.moveGUI == true)
    25. {
    26.  
    27. travelSelection = true;
    28.  
    29. GUI.Box(Rect(1480,750,150,150),"");
    30.  
    31. if (GUI.Button(Rect(1500,850,100,25),GUIContent ("Travel", "Move along the board"))){
    32.  
    33.  
    34.  
    35. if  (waypoint == null)
    36. print ("Select a point to travel");
    37.  
    38. if (shuttleMove == true)
    39. print ("Can only move once per turn");
    40.  
    41. if (distance > 1)
    42. print ("too far to travel");
    43.  
    44. if (shuttleMove == false)
    45. ShuttleSelection.transform.position = waypoint.transform.position;
    46. ShuttleController.moveGUI = false;
    47. shuttleMove = true;
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55. }
    56.  
    57.  
    58. }
    59. }
     
  6. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    put the distance in the update function, the distance is something that needs to be checked each frame, and i don't know if putting the if statement for the distance in the ongui script is a good idea...
     
  7. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    It's still not working, I'm getting a null reference error but that's only because the waypoint isn't set until it is selected.

    NullReferenceException: Object reference not set to an instance of an object
    Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
    Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
    Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
    UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
    Movement..ctor () (at Assets/Scripts/Movement.js:5)
     
  8. kilt

    kilt

    Joined:
    Oct 12, 2011
    Posts:
    136
    well then, place a conditional statement in the update function that will run the distance check if the condition is met.
     
  9. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    I still can't get it too work, same error all the time :(
     
  10. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Whty does it return Boo error messages? Are you sure you created the correct script file type and are using the correct syntax?
     
  11. W4RH4WK117

    W4RH4WK117

    Joined:
    Feb 6, 2011
    Posts:
    129
    It's all in Javascript with correct syntax, no other errors except the null reference once. I believe it was because one of the gameobjects was not set until it was selected but even when selected it will travel any distance.

    I've changed the code a little and moved it back into OnGUI, and this is the error I get now,

    NullReferenceException: Object reference not set to an instance of an object
    Movement.OnGUI () (at Assets/Scripts/Movement.js:32)
     
    Last edited: Apr 23, 2012
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Don't do it on OnGUI. And really - you probably don't have to do it in Update unless it absolutely needs to be checked every frame. But in either case, it's rather straightforward.

    Code (csharp):
    1.  
    2. public class SomeScript : MonoBehaviour {
    3.  
    4.     private float d;
    5.     public float AcceptableDistance;
    6.     public GameObject Shuttle;
    7.     public GameObject Waypoint;
    8.  
    9.     void Update()
    10.     {
    11.         if (Shuttle  Waypoint)
    12.         {
    13.             d = Vector3.Distance(Shuttle.transform.position, Waypoint.transform.position);
    14.             if (d <= AcceptableDistance)
    15.             {
    16.                  // do stuff
    17.             }
    18.         }
    19.     }
    20.  
    21. }
    22.  
     
  13. Seansilla

    Seansilla

    Joined:
    Sep 17, 2020
    Posts:
    9
    Am I missing something on "if(Shuttle Waypoint)"?
     
  14. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    I assume line 11 is meant to be like
    if ((Shuttle != null) && (Waypoint != null))
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Looks like the various forum upgrades over the years ate some of the formatting. I'm guessing I meant
    Code (csharp):
    1.  
    2. if (Shuttle && Waypoint)
    3.  
    because 8 years ago using the boolean implicit "exists" overload was a cool thing to do. (It isn't, don't do that)