Search Unity

Question Find a nearest position

Discussion in 'Code Editors & IDEs' started by Rachan, Oct 23, 2022.

  1. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    783


    Hi there!

    I really want to know How to find next (nearest and should be) box position if I press Up button to move up, as this image if I stay at red circle and want to go to that boxes How to move this red circle to overlap that nearest box?

    Thanks
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Nearest center of a box to the center of the circle is trivial:

    var distance = (circle.transform.position - box.transform.position).magnitude;

    Do this for all boxes and remember the box that (so far) had the smalles distance.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This is the Code Editors and IDE sub-forum and isn't related to your question. I'm wondering why you posted here?