Search Unity

Object Stops at destination and plays animation

Discussion in 'Navigation' started by Ashlyn101, May 17, 2019.

  1. Ashlyn101

    Ashlyn101

    Joined:
    May 25, 2018
    Posts:
    2
    Hi Everyone! This is my first post on here! I'm been looking on and off for a few days now, but I haven't found an answer yet. Just some clarifications, the game is in 3d, the yellow circle represents the object I want to move around (even though there is a 'P' on it, it isn't my player,) and this object moves on its own.
    Okay, so I have a Nav Mesh component on the floor and walls (white = floor; black = walls) and a Nav Mesh Agent on the object I want to move. The object can get from its starting point to all the destinations (the red circle, the pink circle, the brown circle, the blue one, and the orange circle.) I want my object, when it reaches the red destination, to stop, then play a certain animation. When the animation is over, the object should then go to the next point. and have this happen at all the points. (different animations at the other points) Idk if this is too complicated or too complex. If you can help that would be amazing!! If this is too complicated, please tell me because that would also help me out A LOT! Thank you for your time! Oof.jpg
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    That's doable, but not the simplest thing to do.
    You're going to want to write a script that has access to the object's Animator and NavMeshAgent.
    Then, you want to use that script to set the NavMeshAgent's destination.
    Once your agent has reached its destination, you tell the Animator what animation to play with a trigger.
    After all this, you simply need to check when your animation is finished and set a new destination. At this point you probably also want to set the name for the next destination.

    Code (CSharp):
    1. Vector3 destination;
    2. navAgent.SetDestination(destination);
    3.  
    4. void Update()
    5. {
    6. if(navAgent.remainingDistance == 0)
    7. {
    8. animator.SetTrigger("animationTrigger");
    9. }
    10. }
    This is just some quick first draft for the first few steps, you're probably going to want to read up on the Animator to figure out a more elegant way to set and check animations.
     
    aidanbugeja1 and Ashlyn101 like this.
  3. Ashlyn101

    Ashlyn101

    Joined:
    May 25, 2018
    Posts:
    2
    Omg! Thank you so much! This is very helpful. I'll look into this more!