how can I make a sensor in unity ?,how can I make a distance sensor in unity ?,

So I would like to make a sensor in unity to get th distance between the sensor and the nearest obstacle.

I know the function to get the distance between two object but it gets it between the center of these two objects and not to the nearest surface in a straight line.

Is there a build in function to do that ?

and if not how would you do it ?

thanks for the time it took you to answer :),

Instead of finding the distance between the position of those 2 objects, you can first raycast from sensor to obstacle and then, get the distance between sensor position and hit.point of raycast.