2021-04-22 19:42:38 +00:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using UnityEngine;
|
2020-11-20 19:50:31 +00:00
|
|
|
|
|
2021-04-26 13:30:21 +00:00
|
|
|
|
namespace QSB.Animation.Player
|
2020-11-20 19:50:31 +00:00
|
|
|
|
{
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public class PlayerHeadRotationSync : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private Animator _attachedAnimator;
|
|
|
|
|
private Transform _lookBase;
|
2020-12-07 18:49:51 +00:00
|
|
|
|
private bool _isSetUp;
|
2020-11-22 13:57:31 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public void Init(Transform lookBase)
|
|
|
|
|
{
|
|
|
|
|
_attachedAnimator = GetComponent<Animator>();
|
|
|
|
|
_lookBase = lookBase;
|
2020-12-07 18:49:51 +00:00
|
|
|
|
_isSetUp = true;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-11-22 13:57:31 +00:00
|
|
|
|
|
2020-12-03 08:28:05 +00:00
|
|
|
|
private void LateUpdate()
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2020-12-07 18:49:51 +00:00
|
|
|
|
if (!_isSetUp)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-04-22 19:42:38 +00:00
|
|
|
|
if (_attachedAnimator == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - _attachedAnimator is null!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-04-22 19:42:38 +00:00
|
|
|
|
if (_lookBase == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - _lookBase is null!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +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-11-28 09:55:25 +00:00
|
|
|
|
}
|