Search code examples
c#unity-game-enginegame-physics

Getting distance between point of an object and another object


First, I'd like to apologize because this is a very basic and repetitive question, but I am completely new to game development.

I understand that I can find a distance between two objects using:

float dist = Vector3.Distance(other.position, transform.position);

However, how can I find the distance between a point of one object to other object?

For instance let's say my object is this sphere

enter image description here

Now, how can I return an array that says that there are no objects to the left (null), in the front there is an object at 1, and to the right there is an object at 0.5?

Thank you for your patience and understanding


Solution

  • I'm not exactly sure what you wan't to achieve...

    If you wan't to get potential objects at 0.5, 1, 1.5, etc. on lets say the Z Axis you probably would want to do this with raycasting.

    If you wish to check for any objects returning the direction dependant to the Z Axis (0.5, 0.856, 1.45, etc) in contrast, you probably would either

    1. use a scaled sphere collider and add the colliding object with the OnCollisionEnter Callback to the array/List
    2. iterate through every object of the scene and check it's relative pos
    3. Use Raycast Probes procedurally (using a density float and Raycasting every density offset on the Z Axis and checking if the ray hit anything ...)
    4. ...

    Totally dependant on your case of use, and whether you use 2D or 3D.

    Seneral

    EDIT: Here's what you would want in 2D to get a wall withing range maxRange on the layer optionalWallLayer in direction y 1 (up in unity 2D)

     float maxRange = 10.0f;
     RaycastHit hit; 
     Vector3 dir = transform.TransformDirection(new Vector3 (0, 1, 0));
     if (Physics.Raycast(transform.position, dir, maxRange, out hit, LayerMask optionalWallLayer)) 
     {
         Debug.Log ("Wall infront of this object in Range" + hit.distance);
         // hit contains every information you need about the hit
         // Look into the docs for more information on these
     }
    

    This would likely go into the Update Function of your Sphere's MonoBehaviour.

    The option with the Sphere collider is useful if you are not sure if you are going to hit your obstacles. If these are small, you would likely want to add the said Sphere Collider as a component to your sphere, scale it up to your maximum distance, and add a MonoBehaviour script to the sphere, which would contain something like this:

    public List<Transform> allObjectsInRange; // GameObjects to enclose into calculations, basically all which ever entered the sphere collider.
    public List<float> relatedDistances;
    
    void Update () {
        // basic loop to iterate through each object in range to update it's distance in the Lists IF YOU NEED...
        for (int cnt = 0; cnt < allObjectsInRange.Count; cnt++) {
            relatedDistances[cnt] = Vector2.Distance (transform.position, allObjectsInRange[cnt].position);
        }
    }
    
    // Add new entered Colliders (Walls, entities, .. all Objects with a collider on the same layer) to the watched ones
    void OnCollisionEnter (Collision col) {
        allObjectsInRange.Add (col.collider.transform);
        relatedDistances.Add (Vector2.Distance (transform.position,  col.collider.transform));
    }
    
    // And remove them if they are no longer in reasonable range
    void OnCollisionExit (Collision col) {
        if (allObjectsInRange.Contains (col.collider.transform)) {
            relatedDistances.RemoveAt (allObjectsInRange.IndexOf (col.collider.transform));
            allObjectsInRange.Remove (col.collider.transform);
        }
    }
    

    That should do it, either of the options, based on your exact case, again:) Note both are pseudo-codes... Hope it helps!