Search Unity

Generating a path between two points

Discussion in 'Scripting' started by MinorMapillai, Sep 7, 2019.

  1. MinorMapillai

    MinorMapillai

    Joined:
    Jun 29, 2018
    Posts:
    3
    I am trying to make a path procedurally using prefabs.
    Right now, I have a plane and there are objects on it. The path must be randomly generated every time around the obstructions that are on the plane.
    I've looked at NavMesh but I don't completely understand how to use it.
    Is there another way to do it?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,072
    If you can divide the available space into a grid or other discrete waypoints, then you can treat it as a graph and apply any of the standard graph-based route algorithms like breadth-first search, depth-first search, A*, etc. depending on your requirements.

    Of course, if you're not trying to find the shortest path and instead just want some path, then how you choose among the possible paths is mostly a stylistic choice. One simple option might be something like:
    1. Choose a random node that is adjacent to the end of the path-so-far
    2. Check if any path exists from that node to the destination without crossing the path-so-far
    3. If so, add that node and loop; if not, choose a different adjacent node.
     
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    169
    What do you mean "make a path"

    If you want AI to navigate through your world and around obstacles, I would definitely use navmesh. Brackeys has some good tutorials on it.
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    203
    1. Use the component-based NavMesh system.
    2. Arrange a set of dummy obstructions you can turn on and off that obstruct gaps between your actual obstructions.
    3. Randomly rescale and activate your dummy obstructions.
    4. Compute a navmesh.
    5. Compute a path. This is your "random" path that you will have at least some stylistic control over.
    6. Deactivate the dummy obstructions.
    You can also randomly generate a marching line of circles of various widths along the navmesh path you compute with just the real obstructions, and choose random points inside the circles. This is a little harder to do the math for though.
     
  5. MinorMapillai

    MinorMapillai

    Joined:
    Jun 29, 2018
    Posts:
    3
    I need to generate a path that is navigable around certain obstacles on the path
     
  6. MinorMapillai

    MinorMapillai

    Joined:
    Jun 29, 2018
    Posts:
    3
    How will I place the objects I have (as prefabs) along the path?
    The way I understand it, the NavMesh will find a path for the object it is attached to and then it just moves that object along the path it finds.
    Is that wrong?
     
  7. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    203
    Not really what you were asking for in the original question... But you can get the Vertex3[] array of path points from an agent, and place prefabs that way.