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.

NavMeshBuildSource for BoxCollider produce incorrect rotation

Discussion in 'Navigation' started by Daft_Hunk, Oct 31, 2018.

  1. Daft_Hunk

    Daft_Hunk

    Joined:
    Nov 15, 2016
    Posts:
    15
    Hello, I'm using the NavMeshComponents and I want to optimize the generation by feeding myself the sources as I'm working on a Strategy game and their are placed by the player at runtime.

    However I want to switch from the Mesh collection method to the Collider one.

    So I made this code for the Box shape (I now it's not pretty but it's just for testing purpose)

    Code (CSharp):
    1. src.shape = NavMeshBuildSourceShape.Box;
    2. src.size = _source.GetComponent<BoxCollider>().bounds.size;
    3.  
    4. Quaternion rotation = Quaternion.Euler(0,0,0);
    5.  
    6. src.transform = Matrix4x4.TRS(
    7.     _source.GetComponent<BoxCollider>().bounds.center,
    8.     rotation,
    9.     Vector3.one);
    However if it's working well for 0 and 90° rotation, for other one it seems like the box isn't rotated...


    I'm I doing something wrong ?
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    488
    Well you seem to always apply a rotation of (0,0,0), so it will probably just take the bounds as they currently are.
    That said, I'm not entirely sure why you're doing this? Wouldn't it be a lot more efficient to have a specific layer in your scene where NavMesh-relevant objects go and whenever the content of this layer changes, force a rebuild? Unity will already only update the cells of the NavMesh were geometry changed.
     
  3. Daft_Hunk

    Daft_Hunk

    Joined:
    Nov 15, 2016
    Posts:
    15
    Yeah the (0,0,0) rotation is due to the fact I want it to be at the same rotation of it's parent (it's only the parent who get the rotation)
    If I change the rotation here it is added to the parent rotation so it end to be offset :)
    I understand what you are saying but sadly I'm already using the layers for other systems and making my own source gathering is perfect in my case and allow me to keep track of everything easily but thanks for the advice ;)
     
  4. Daft_Hunk

    Daft_Hunk

    Joined:
    Nov 15, 2016
    Posts:
    15
    Hello again ! Thanks for your answer it made me rethink on how I was doing my method and the problem was due to the fact I was getting the bounds instead of getting the collider.size and after applying the rotation.

    Just in case someone is getting the same problem here is the final code :

    Code (CSharp):
    1.  
    2. src.size = _source.GetComponent<BoxCollider>().size;
    3. src.transform = Matrix4x4.TRS(
    4.       _source.bounds.center,
    5.       _source.transform.parent.rotation,
    6.       Vector3.one);
    7.