Search Unity

Continue walk animation during off-mesh link

Discussion in 'Navigation' started by kaffiene, Oct 2, 2017.

  1. kaffiene

    kaffiene

    Joined:
    May 26, 2013
    Posts:
    21
    I'm using an off-mesh link to simulate a one-way door. I've written code to manage translating the agent during traversal of the link. The only issue is that the standard walking animation is turned off when link traversal is occurring. Is there any way I can have the walk animation just continue while the link is being traversed?
     
  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Yes you can sir. There is on the documentation a page on how to do it, but i'm unable to find it. So i will explain :

    First you need to tel your agent to not auto traverse links :

    Code (CSharp):
    1. var agent = GetComponent<NavMeshAgent>();
    2.             agent.autoTraverseOffMeshLink = false;
    3.  
    Now you need to check when your agent is or not on a offmeshLink :

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.    if (agent.isOnOffMeshLink)
    5.             {
    6.                //Do what you want I use a function : NormalSpeed()
    7.             }
    8. }
    9.  
    With that you have total control on the agent when he is on the link. You need to move manually the agent to the end of the link.

    Code (CSharp):
    1.  
    2. //With this you can acces the start and the endpoint of the current offmeshlink
    3. OffMeshLinkData data = agent.currentOffMeshLinkData;
    4.  
    5.             //calculate the final point of the link
    6.             Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
    7.  
    8.             //Move the agent to the end point
    9.             agent.transform.position = Vector3.MoveTowards(agent.transform.position, endPos, agent.speed * Time.deltaTime);
    10.  
    11.             //when the agent reach the end point you should tell it, and the agent will "exit" the link and work normally after that
    12.             if(agent.transform.position == endPos)
    13.             {
    14.                 agent.CompleteOffMeshLink();
    15.             }
    16.  
    Hope it help you !