Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Official Head-turning NPCs

Discussion in 'Open Projects' started by randomscribe, Jun 30, 2021.

  1. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    cirocontinisio likes this.
  2. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    Nothing that I can think of. Are you familiar with Animation Rigging?
     
  3. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    Nope! Yesterday I got to a point where I had a MultiAimConstraint operating on a townsfolk's head to make him look at a rock or something as he walked around, but there was some screwiness with the axes and he kept turning his head so his left ear was pointing at the aim target, not his face. :(

    And I'm not sure if that's the right approach anyway because I can't seem to update the MultiAimConstraint's source object at runtime, and there's not a player object in the scene itself to link it to in the editor...

    So yeah, bit of a learning curve. Any guidance would be welcome. :)
     
  4. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    The sequence of steps as I currently understand it:
    1. Add a Rig Builder to the top-level townsfolk object, on the same level as the Animator
    2. Add a child with a Rig component and tell the Rig Builder to use it
    3. Add children to the object with the Rig and give them Constraint components set up to [???]
    4. Use scripts to control the behavior of the Constraints by [???]
     
  5. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    Update: I figured out what I was doing wrong with the axes, and I can now get a villager to properly look in the direction of something. :)
     
  6. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    The constraints of AnimationRigging activate (on Start I think) and create a job with all the parameters including the target Transform, so that the constraint can run multi-threaded. Once the job has been created, changing them from the Inspector has no effect, unless you call some API to recreate the job.

    One thing you can do is to add a child object as the target, and have a script (maybe a StateMachine action...?) update this object's position to the character head. Then the constraint would rotate the bone appropriately.
     
  7. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    Ok, that all makes sense! Thanks.

    In the meantime, I've figured out how to constrain multiple nodes so that the villager twists his upper body a bit to look at things, rather than just eerily turning his head back and forth.

     
    cirocontinisio likes this.
  8. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    Update: I've got the villager looking at the player when the player is in his field of vision, as defined by a trigger zone. The problem now is that he turns his head instantly when the player enters/exits the zone.

     
    cirocontinisio likes this.
  9. randomscribe

    randomscribe

    Joined:
    Jun 14, 2021
    Posts:
    29
    Okay, really happy with where this is at now. Gonna call it a day. :)

     
  10. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    Nice! :)
    You could probably make the head turn a bit slower or make the trigger less generous. But it's already looking super cool!
     
  11. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    I actually made a video on the subject last year, if you want to go a bit deeper on Animation Rigging (although some basics you already covered):




    Also, just noticed that Matt Gambell from Game Dev Guide posted a video related to this, if you're interested. I think he talks more about using it with Timeline, and it could be that he doesn't use Unity's Animation Rigging package but a custom one (haven't watched it through):

     
    MortyJHin and Smurjo like this.