Search Unity

FPS - Move Head where Player looks

Discussion in 'Animation' started by illuminatigaming, Jan 16, 2016.

  1. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Hello,
    i am working on a PUN-Multiplayer FPS.
    I want that the head (or neck, the results should look similar) moves up and down with the FPS camera.
    I tried to rotate the head transform, but it seems to interfere with the idle animation.
    So the head doesnt care and it doesnt rotate like i want, it rotates only like the idle animation rotates it.:(
    I tried to do a animation for the head, too.
    In the animation menu i created a new animation, added the heads transform as component and in the last keyframe i changed its rotation, but it jumps back to the default values in the animation menu.
    I'm new to Unity, i hope this is the right section, and i hope you understand my description. :)
    Sincerely,
    IlluminatiGaming
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    For that you usually use IK (inverse kinematics). See IK tutorials and use http://docs.unity3d.com/ScriptReference/Animator.SetLookAtPosition.html
    Simple code should be
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HeadRotation : MonoBehaviour
    4. {
    5.     public Transform target;
    6.     Animator anim;
    7.     void Start()
    8.     {
    9.         anim = GetComponent<Animator>();
    10.     }
    11.     void OnAnimatorIK()
    12.     {
    13.         anim.SetLookAtPosition(target);
    14.     }
    15. }
    Make sure in your Animator Controller layer's IK is enabled (in layers small gear - "IK Pass")
    P.S. Didn't test code. Before 5.0 unity it required PRO version if you're using old versions.
     
  3. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Thanks for your answer.
    I've heard of IK, but i thought it would still require Unity Pro.
    I managed to got the head to work.
    I had to add Animator.NeckDownUP or something similar and the click on record, and go to the last animation keyframe and next to the Name i could type in a little uppercase number.
    So i created one animation looking up, one looking forward and one looking down.
    Smashed togehter in a blend tree.
    The value for the blend tree is then calculated from camera.transform.localRotation.
    thanks for the answer, will be probably useful with more complex stuff :)
     
  4. PrMoustache

    PrMoustache

    Joined:
    Jan 3, 2018
    Posts:
    2
    COOL ! Thanks Man!