quantum-space-buddies/QSB/Animation/Player/PlayerHeadRotationSync.cs

44 lines
1.0 KiB
C#
Raw Normal View History

2021-04-22 19:42:38 +00:00
using OWML.Common;
using QSB.Utility;
using UnityEngine;
2020-11-20 19:50:31 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Animation.Player;
public class PlayerHeadRotationSync : MonoBehaviour
2020-11-20 19:50:31 +00:00
{
2022-03-03 03:46:33 +00:00
private Animator _attachedAnimator;
private Transform _lookBase;
private bool _isSetUp;
public void Init(Transform lookBase)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
_attachedAnimator = GetComponent<Animator>();
_lookBase = lookBase;
_isSetUp = true;
}
2020-11-22 13:57:31 +00:00
2022-03-03 03:46:33 +00:00
private void LateUpdate()
{
if (!_isSetUp)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2020-12-02 21:23:01 +00:00
}
2020-11-22 13:57:31 +00:00
2022-03-03 03:46:33 +00:00
if (_attachedAnimator == null)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - _attachedAnimator is null!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +00:00
if (_lookBase == null)
{
DebugLog.ToConsole($"Error - _lookBase is null!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +00:00
var bone = _attachedAnimator.GetBoneTransform(HumanBodyBones.Head);
// Get the camera's local rotation with respect to the player body
var lookLocalRotation = Quaternion.Inverse(_attachedAnimator.transform.rotation) * _lookBase.rotation;
bone.localRotation = Quaternion.Euler(0f, 0f, lookLocalRotation.eulerAngles.x);
2020-12-02 21:23:01 +00:00
}
2020-11-28 09:55:25 +00:00
}