mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-10 06:51:36 +00:00
58 lines
1.2 KiB
C#
58 lines
1.2 KiB
C#
using QuantumUNET;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace QSB.Animation.Player
|
|
{
|
|
public class CrouchSync : QNetworkBehaviour
|
|
{
|
|
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
|
|
|
|
private const float CrouchSmoothTime = 0.05f;
|
|
private const int CrouchLayerIndex = 1;
|
|
|
|
private PlayerCharacterController _playerController;
|
|
private Animator _bodyAnim;
|
|
|
|
[SyncVar]
|
|
private float _crouchValue;
|
|
|
|
public void Init(PlayerCharacterController playerController, Animator bodyAnim)
|
|
{
|
|
_playerController = playerController;
|
|
_bodyAnim = bodyAnim;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (IsLocalPlayer)
|
|
{
|
|
SyncLocalCrouch();
|
|
return;
|
|
}
|
|
SyncRemoteCrouch();
|
|
}
|
|
|
|
private void SyncLocalCrouch()
|
|
{
|
|
if (_playerController == null)
|
|
{
|
|
return;
|
|
}
|
|
var jumpChargeFraction = _playerController.GetJumpChargeFraction();
|
|
_crouchValue = jumpChargeFraction;
|
|
}
|
|
|
|
private void SyncRemoteCrouch()
|
|
{
|
|
if (_bodyAnim == null)
|
|
{
|
|
return;
|
|
}
|
|
CrouchParam.Target = _crouchValue;
|
|
CrouchParam.Smooth(CrouchSmoothTime);
|
|
var jumpChargeFraction = CrouchParam.Current;
|
|
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
|
|
}
|
|
}
|
|
} |