how do i calculate distance while taking in account the size of a object.

i’m making a space game in which the player can command unit to defend/attack/repair a target. the problem is that the size different between the two objects can be gigantic and vector 3 distance only take in the account the space between the two object center.

i was thinking about using a raycast to get the proper distance from the surface but I also have to worry about the size of the moving object as well.

use the bounding box.

A bounding box is a box all gameobjects have which is the smallest SQUARE/BOX possible that encompases the shape completely. you can use bounds.extents to basically get a distance that is roughly basically from the center to the edge of an object. ROUGHLY.

It could be a decent ways past it depending on the shape but it should get you close and your just following. If you take the distance between the 2 objects and subract from it both objects bounds.extents you should get them pretty close togehter.

I manage to figure it out, for anyone run into a similar problem this is the code i’m using

C# code

vector3 orbitPosition; RaycastHit ship;
        if (Physics.Linecast(this.gameObject.transform.position, target.transform.position, out ship))
            orbitPosition = ship.point;
            distance = Vector3.Distance(this.transform.position, orbitPosition) - (.5f * this.gameObject.transform.lossyScale.z);

to give a minor explanation, i use a linecast with raycasthit.point to find the surface the targeted object. From there i subtracted the space between the targeting gameobject(the object this script is attached to) and it surface using space allowing distance to read 0 should surface of the two object come into contact.