Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

FPS - Move Head where Player looks

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

  1. illuminatigaming


    Jan 16, 2016
    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. :)
  2. Teravisor


    Dec 29, 2014
    For that you usually use IK (inverse kinematics). See IK tutorials and use
    Simple code should be
    Code (CSharp):
    1. using UnityEngine;
    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


    Jan 16, 2016
    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


    Jan 3, 2018
    COOL ! Thanks Man!