Search Unity

Navmesh for Random Generated Maze

Discussion in 'Navigation' started by RobertOne, Jun 5, 2015.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    Hey!
    Iam currently trying to do a simple maze Runner and using navmeshes for the movement. so a simple maze part looks like this:

    and in this are 3 child gameobjects with each a navmeshobstacle(carved) on it

    my scene looks like this:


    when the game starts, a simple maze gets generated:

    when i click nearby my player, he moves to the destinated point. but as soon as i click a little bit further away, he tries to run threw the wall and never reaches his destination. which looks like this in the editor mode:


    ingame it looks like this:

    sadly my gif-grabbing program doesent record clicks but on the gif above i am clicking pretty much all the time. and as you can see, the ball/player is moving when i click nearby, but as soon as i click on the lower right, he ignores it.

    what could cause this behavior? did i missed something? if you need more screenshots, just let me know :)
    ps: when the scene is empty, i can move all over the place, so this is working. here also some screens of my navmesh agent setting and the navmesh obstacle settings:

     
  2. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    I dont like to do this but:
    *bump* still need help :/
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Try Obstacle Avoidance Quality to high quality ..

    Also add this script to the ball so you can see the path generated .. the target will be the mouse click position.

    Script supplied by Jakob_Unity

    Code (CSharp):
    1. //CalculatePathTest.cs
    2. using UnityEngine;
    3. [ExecuteInEditMode]
    4. public class CalculatePathTest : MonoBehaviour {
    5.     public Transform target;
    6.     void OnDrawGizmos () {
    7.         if (target == null) return;
    8.        
    9.         var path = new NavMeshPath();
    10.         NavMesh.CalculatePath (transform.position, target.position, NavMesh.AllAreas, path);
    11.        
    12.         Gizmos.color = Color.red;
    13.         Gizmos.DrawRay (transform.position, Vector3.up);
    14.         Gizmos.DrawRay (target.transform.position, Vector3.up);
    15.         Gizmos.color = Color.green;
    16.         var offset = 0.2f * Vector3.up;
    17.         for (int i = 1; i < path.corners.Length; ++i)
    18.             Gizmos.DrawLine (path.corners[i-1] + offset, path.corners[i] + offset);
    19.     }
    20. }
     
  4. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    thanks, but i got two error on line 10
    Code (CSharp):
    1. NavMesh.CalculatePath (transform.position, target.position, NavMesh.AllAreas, path);
    NavMesh does contain a definition for "CalculatePath"
    and
    NavMesh does contain a definition for "AllAreas"
     
    Last edited: Jun 7, 2015
  5. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    so i found this code here in the interwebs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PathUtils : MonoBehaviour {
    5.     private NavMeshAgent agent;
    6.     private Color c = Color.white;
    7.     public void Start() {
    8.         agent = gameObject.GetComponent<NavMeshAgent> ();
    9.     }
    10.    
    11.     public void Update() {
    12.         DrawPath (agent.path);
    13.     }
    14.    
    15.     void DrawPath(NavMeshPath path) {
    16.         if (path.corners.Length < 2)
    17.             return;
    18.         switch (path.status) {
    19.         case NavMeshPathStatus.PathComplete:
    20.             c = Color.white;
    21.             break;
    22.         case NavMeshPathStatus.PathInvalid:
    23.             c = Color.red;
    24.             break;
    25.         case NavMeshPathStatus.PathPartial:
    26.             c = Color.yellow;
    27.             break;
    28.         }
    29.        
    30.         Vector3 previousCorner = path.corners[0];
    31.        
    32.         int i = 1;
    33.         while (i < path.corners.Length) {
    34.             Vector3 currentCorner = path.corners[i];
    35.             Debug.DrawLine(previousCorner, currentCorner, c);
    36.             previousCorner = currentCorner;
    37.             i++;
    38.         }
    39.        
    40.     }
    41. }
    and i think its doing the same as you code :) at least it draws a line and show me the path, i also set Obstacle Avoidance Quality to high quality. And, i kicked out the random generated part for the beginning. so the maze looks every time the same:


    but still, the agent is not able to find his end destination. he somehow stucks in the wall at some point.
     
  6. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Try upping the size of the Agent Radius in the bake settings looks like not enough gap around the edge of the maze walls ..
     
  7. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    still no success :/




    it somehow always stuck at the same spot.
    with radius 2 it also stops there.
     
  8. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Have you a rigid body on the ball? if so remove it and try that.
     
  9. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    no, just my line navmesh agent and my navmesh script


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NavMesh : MonoBehaviour {
    4.     NavMeshAgent agent;
    5.    
    6.     void Start() {
    7.         agent = GetComponent<NavMeshAgent>();
    8.     }
    9.    
    10.     void Update() {
    11.         if (Input.GetMouseButtonDown(0)) {
    12.             RaycastHit hit;
    13.            
    14.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
    15.                 agent.destination = hit.point;
    16.             }
    17.         }
    18.     }
    19. }
    20.  
     
  10. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    ahh, damn, there where some random navmesh obstacles in my scene which i didnt removed and they where on top of the maze. so stupid! but thanks for your help!