Skip to content

Instantly share code, notes, and snippets.

@Apsu
Created March 10, 2020 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apsu/8c0c574c266769ec780cb431b909ebeb to your computer and use it in GitHub Desktop.
Save Apsu/8c0c574c266769ec780cb431b909ebeb to your computer and use it in GitHub Desktop.
Unity HeadLook
using UnityEngine;
public class HeadLook : MonoBehaviour
{
[Header("Configs")]
[Range(0.0f, 1.0f)]
public float headLookWeight;
private Animator animator;
private Vector3 lookAtPosition;
private Camera mainCam;
private void Awake()
{
animator = GetComponent<Animator>();
mainCam = Camera.main;
}
private void Update()
{
Vector3 camForward = mainCam.transform.forward * 10;
// Yaw
lookAtPosition.x = camForward.x;
// Clamp pitch from horizon to up
lookAtPosition.y = Mathf.Clamp(camForward.y, 0.0f, 1.0f);
lookAtPosition.z = camForward.z;
}
private void OnAnimatorIK(int layerIndex)
{
Transform head = animator.GetBoneTransform(HumanBodyBones.Head);
// Default weights apply to head only
animator.SetLookAtWeight(headLookWeight);
animator.SetLookAtPosition(head.position + lookAtPosition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment