60 lines
1.2 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Utility.VariableSync;
using UnityEngine;
2021-04-26 14:30:21 +01:00
namespace QSB.Animation.Player
{
public class CrouchSync : NetworkBehaviour
2020-12-02 21:29:53 +00:00
{
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
2020-12-02 21:29:53 +00:00
private const float CrouchSmoothTime = 0.05f;
private const int CrouchLayerIndex = 1;
2020-12-02 21:29:53 +00:00
private PlayerCharacterController _playerController;
private Animator _bodyAnim;
2021-11-26 19:33:56 +00:00
public FloatVariableSyncer CrouchVariableSyncer;
2021-05-03 20:13:03 +01:00
public void Init(PlayerCharacterController playerController, Animator bodyAnim)
2020-12-02 21:29:53 +00:00
{
_playerController = playerController;
_bodyAnim = bodyAnim;
}
2020-12-14 21:41:56 +01:00
public void Update()
2020-12-02 21:29:53 +00:00
{
if (isLocalPlayer)
2020-12-02 21:29:53 +00:00
{
SyncLocalCrouch();
return;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:29:53 +00:00
SyncRemoteCrouch();
}
2020-12-02 21:29:53 +00:00
private void SyncLocalCrouch()
{
if (_playerController == null)
{
return;
}
2021-06-18 22:38:32 +01:00
2021-10-12 15:31:02 +01:00
var jumpChargeFraction = _playerController.GetJumpCrouchFraction();
2021-12-14 21:42:40 -08:00
CrouchVariableSyncer.Value = jumpChargeFraction;
2020-12-02 21:29:53 +00:00
}
2020-12-02 21:29:53 +00:00
private void SyncRemoteCrouch()
{
if (_bodyAnim == null)
{
return;
}
2021-06-18 22:38:32 +01:00
2021-12-14 21:42:40 -08:00
CrouchParam.Target = CrouchVariableSyncer.Value;
2020-12-02 21:29:53 +00:00
CrouchParam.Smooth(CrouchSmoothTime);
var jumpChargeFraction = CrouchParam.Current;
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
}
}
2020-11-08 16:13:10 +00:00
}