Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get CLOSEST Vector3 to another Vector3 but a minimum of X units farther from a list of Vector3's

Discussion in 'Scripting' started by Milionario, Mar 3, 2021.

  1. Milionario

    Milionario

    Joined:
    Feb 21, 2014
    Posts:
    119
    I hope that was clear enough.

    I have a Vector3 called destination.
    I have a list of Vector3's called possible destinations.
    I have a list of Vector3's called obstacles.

    If my destination is closer than X from any obstacle in the list:
    Find a Vector3 in the possible destinations list that is closest to the destination but still farther than X from all obstacles?
    upload_2021-3-3_15-52-18.png
     

    Attached Files:

    Last edited: Mar 3, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    It sounds like a two-step filter. I imagine you would just iterate your 'possible destinations list' and see which ones fit the criteria, then choose the closest one.
     
  3. Milionario

    Milionario

    Joined:
    Feb 21, 2014
    Posts:
    119
    So probably ordering them by distance in the first pass, then finding the first one that is X units away from the obstacles? or even the other way around should work i guess
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Sure... if there's only a few, either order would probably be fine.

    Usually you want to reduce your dataset first, THEN sort it, just to reduce computation expense

    But if it's only a few items, eh, probably be fine either way.
     
    Joe-Censored and Milionario like this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I imagine you'll also need to handle the case where no Vector3 in the possible destinations list meets the criteria.
     
    Milionario likes this.