look at closer

hey everyone, i need some help, i want my capsule look at cube, but i want look at cube closer to my capsule, i just know script : transform.LookAt ("Cube");, but it’s not look at closer to my capsule, so i need help to look at closer to my capsule. I’ve been looking for a solution on youtube, but did not find any solution
thanks,

Is the amount of cubes always the same, or set from the start?

If so, an easy way to do this is to store all the cubes in an array on your script (and set them through the inspector), and do a distance check across them to find which is the closest, followed by doing a transform.lookat to that closest cube.

(Even if it isn’t, you can apply a similar method, you just need to update that array of cubes, and most likely use a List instead of a static array)