Created
March 10, 2020 04:15
-
-
Save Apsu/8c0c574c266769ec780cb431b909ebeb to your computer and use it in GitHub Desktop.
Unity HeadLook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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