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

NavMesh in a moving train?

Discussion in 'Navigation' started by Griffo, Feb 20, 2016.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi, I have a moving train that I want some people to walk out of when it pulls into a station, but is it possible to have a NavMesh in the moving train?
     
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    707
    So although you cant currently bake navigation to dynamically moving objects, you should be able to setup a work around for what you need. Maybe try this...
    *Go to Navigation > Areas and add an area (Call it something like "Train").
    *Now in your scene move the train GameObject to its destination where the people will walk off.
    *In Navigation > Object select the train and set its navigation area to "Train", and Navigation Static checked.
    *You should now be able to bake your navigation Navigation > Bake and select the bake button on the bottom.
    *Select all Navmesh agents, and goto area mask and uncheck the "Train" so it can not be used, since we only want the agents to be able to walk on this area when there is a train in place.
    *Reset your train GameObject to it normal starting position and uncheck navigation static.
    *Next will require a bit of coding. Use a trigger collider on the train tracks that will be hit right as the train reaches its destination. OnTriggerEnter find all the Navmesh agents that will be entering or exiting the train. with all of these agents we will now get the area mask and check the "Train" area to checked. set the agents destination and they should walk off or on the train.
    *When or if the train leaves just uncheck all the agents "Train" Area again, so we don't have people walking in mid air over the train tracks.

    Hope that helps.
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @JBR games Thank you for that full explanation, I was already moving the train to the station an baking the NavMesh but did not think about area masks, thank you that will work great.