Search Unity

If path exists (pathfinding)

Discussion in 'Scripting' started by ArtLove, Nov 18, 2017.

  1. ArtLove

    ArtLove

    Joined:
    Oct 16, 2012
    Posts:
    49
    Hello, I'm looking for a way to do simple pathfinding in c#.

    I have 2 rooms (a and b) with a door separating them. They can change shape at any time. The rooms are made of 1x1x1 cubes and one room (room b) has a table.

    While in room a I want to see if a path exists to room B's table. Of course if the door is closed and there are no breaches that opens room a to b, then it would come back as false.

    Could somebody point me in the right direction for pathfinding?
     
  2. ghostmode

    ghostmode

    Joined:
    Sep 26, 2016
    Posts:
    14
    Check out Unity's nav mesh. There's an official tutorial here:
    https://unity3d.com/learn/tutorials...uction-and-navigation-overview?playlist=17105

    Once you're familiar with it, you can check if a valid path exists between an agent and a target position like so:
    Code (CSharp):
    1. navMeshAgent.CalculatePath(targetPosition, navMeshPath);
    2.  
    3. if (navMeshPath.status == NavMeshPathStatus.PathComplete) {
    4.     // There is a valid path to the target positon.
    5. }
     
  3. ArtLove

    ArtLove

    Joined:
    Oct 16, 2012
    Posts:
    49
    Thanks for the reply!

    So it sounds like navmesh would work for instance where rings are built in runtime by players? You don't have to create predefined paths?
     
  4. ghostmode

    ghostmode

    Joined:
    Sep 26, 2016
    Posts:
    14
    One thing I should have asked first, are the cubes all side-by-side (dungeon keeper style)? Or are they also stacked on top of each other (minecraft style)? How many cubes are in each room exactly?

    No you don't need to make predefined paths. You can build nav meshes at runtime:
    https://unity3d.com/learn/tutorials/topics/navigation/baking-navmesh-runtime

    One approach would be to have a single nav mesh surface for the scene, and use obstacle carving to allow the cubes to modify the nav mesh at runtime:
    https://docs.unity3d.com/Manual/class-NavMeshObstacle.html
     
  5. ArtLove

    ArtLove

    Joined:
    Oct 16, 2012
    Posts:
    49
    The cubes are stacked Minecraft style. The rooms vary in size from 100 to 500.
     
  6. ghostmode

    ghostmode

    Joined:
    Sep 26, 2016
    Posts:
    14
    Ah ok, I've never done any minecraft/voxel style pathfinding I'm afraid. Perhaps someone else with experience in his area can weigh in?
     
  7. ArtLove

    ArtLove

    Joined:
    Oct 16, 2012
    Posts:
    49
    Just following up to make sure navmesh is suitable for a Minecraft styled world with building