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

NPC should look at Players direction when player is near

Discussion in 'Scripting' started by Cellenseres, Oct 19, 2017.

  1. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    67
    Hey Guys,

    can you help me out a bit?
    I want the NPCs to look at the Player and follow his moves when player is near the NPC.
    Like this example from the Game "Final Fantasy XIV - A Realm Reborn"


    This is a part of my Scene currently.


    The NPCs has idle animations.
    Is there a way to let the NPC look at the player without interupting the idle animation?
    The NPC should not look at the players position when player is not in the field of sight of the NPC

    Could you give me a little code sample?
    Thank you very much!
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Asking for code samples is not the best way to engage the unity community.

    Whilst we do enjoy helping other developers, noone likes being asked to do your work for you. Seeing as this is such a simple concept that is dealt with in multiple of the official unity tutorials, its pretty apparent that rather than trying to learn this on your own your just cutting corners, and likely wont get many useful responses.

    Also try to remember that if your given the answer, you probably wont really learn what your doing and instead will just know how to "parrot" stuff back (effectively becoming a glorious copy paster).

    https://unity3d.com/learn/tutorials follow some of these, if you make it through a couple you will already know what your asking to do.

    Essentially though there are a number of ways. You could have different mecanim layers for the head and body and then affect one but not the other.

    You could let the animation play but rotate the transform / rigidbody to face the player, you could timeline it with playable api and get hooks to that, etc etc
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    A common approach to solving problems is using a "divide and conquer" method: splitting a problem into smaller bits.

    From what I see you have two problems:

    1) How get the distance between two GameObjects
    2) Make character look at a specific object.

    Answers:
    1) Use Vector3.Distance() or collision checking
    2) Search for Inverse Kinematics (IK for short)

    Asking for a code sample will not help you in a long run.
     
    Cellenseres likes this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    It looks like your NPC is floating, which makes it easier because you don't have to worry about animating footsteps or IK; you can just rotate it. However, if you do need to handle animation, I recommend the suggestions posted above.

    If you only need to rotate your floating NPC to face the player, I'll suggest some things that might point you to the right subjects to read up on. One way is to add a trigger collider that defines the NPC's field of sight. Use the OnTriggerEnter method to set a bool variable true, and the OnTriggerExit method to set the bool false. In the Update method, if the bool is true, use Vector3.Lerp to rotate gradually to toward the player based on the value of Time.deltaTime.

    Now this will rotate the NPC whenever the player is in the trigger collider. You may want to only rotate when the NPC has clear line of sight. To do this, add an empty child GameObject that represents the NPC's eye location. In Update, use Physics.Linecast from the eye location to the player to determine line of sight. If it hits the player, then continue with the Vector3.Lerp.

    This should be a good start if you're just getting into scripting.