60 lines
1.2 KiB
C#
Raw Normal View History

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