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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I determine if there's obstacle between 2 or more players?

Discussion in 'Scripting' started by voyager3, Jun 2, 2017.

  1. voyager3

    voyager3

    Joined:
    May 15, 2017
    Posts:
    8
    In my scene, I have some buildings in the playground.

    How can I determine if there's obstacles between 2 players? The definition of obstacles is things that players cannot go through.

    Also, I want to find the shortest route from A to B. In my opinion, I should apply A* algorithm on tiles that player can walk on. How can I get available tiles?

    Thank you for you help.
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    voyager3 likes this.
  3. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    ^^^ What Iarku said about raycasts.

    If you're rolling your own pathfinding, it depends on how you created the tiles. Are they generated procedurally, or are they manually placed down by you?

    If it's procedural and they're on a regular grid, you could place a script on the tile prefab and use that to keep track of neighbours as the tiles are being built. Then you can keep a List<Node> of all tiles, adding to the list by using tile.GetComponent<Node>();

    Alternatively, if the tiles are static you could use Unity's Navmesh and call the CalculatePath function to get a path from A to B.
     
    larku likes this.
  4. voyager3

    voyager3

    Joined:
    May 15, 2017
    Posts:
    8
    larku likes this.