Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player following a NPC

Discussion in 'Navigation' started by plaklijm, Jul 29, 2020.

  1. plaklijm

    plaklijm

    Joined:
    Nov 23, 2017
    Posts:
    2
    In my game I want you to follow a NPC that has a set path. This NPC waits for you if your too far away and it needs to play a walking animation and when waiting an idle animation. The only problem is that i havent found anything on the internet explaining how you can achieve this, so if anyone can help, please answer
     
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    To stop when the player is too far away: https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-isStopped.html

    Animation: You would use the agent's speed to control the animation https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-velocity.html

    In Mecanim what I do is I pass in the speed as animation parameters. If the speed is zero, play standing animation. If speed is max -> walking animation. The velocity has components, which is useful for different walking animations. If your NPC is walking sideways (maybe they are firing at monsters while moving to the destination) you can make them walk sideways or backwards. Mecanim makes all this pretty easy and there are many tutorials.