Files
Fishing2/Assets/Scripts/Fishing/Player/PlayerAnimator.cs
2025-12-23 18:03:53 +08:00

88 lines
2.8 KiB
C#

using System;
using UnityEngine;
namespace NBF
{
public class PlayerAnimator : MonoBehaviour
{
public Animator Animator { get; private set; }
public FPlayer Player { get; private set; }
private bool _isTorsoLayerEnabled;
private bool _isInit;
#region
public static readonly int IsSwiming = Animator.StringToHash("Swim");
public static readonly int ThrowFar = Animator.StringToHash("ThrowFar");
public static readonly int BoatDriving = Animator.StringToHash("BoatDriving");
public static readonly int BaitInWater = Animator.StringToHash("BaitInWater");
public static readonly int HeldRod = Animator.StringToHash("HeldRod");
public static readonly int RodArming = Animator.StringToHash("RodArming");
public static readonly int Forward = Animator.StringToHash("Forward");
public static readonly int Turn = Animator.StringToHash("Turn");
public static readonly int OnGround = Animator.StringToHash("OnGround");
public static readonly int RodRight = Animator.StringToHash("rod right");
public static readonly int RodForward = Animator.StringToHash("rod forward");
public static readonly int PreciseCast = Animator.StringToHash("Precise Cast");
public static readonly int PreciseIdle = Animator.StringToHash("Precise Idle");
public static readonly string Torso = "Torso";
#endregion
public void SetModelAsset(FPlayer player)
{
_isInit = true;
Player = player;
Animator = player.ModelAsset.Animator;
}
private void LateUpdate()
{
if (!_isInit) return;
UpdateAnimator();
}
private void UpdateAnimator()
{
Animator.SetBool(OnGround, Player.Data.IsGrounded);
float value3 = Mathf.Lerp(Animator.GetFloat(Forward), Player.Data.Speed / 5f, Time.deltaTime * 20f);
Animator.SetFloat(Forward, value3);
// 平滑处理
// float smoothedTurn = Mathf.SmoothDamp(Animator.GetFloat(Turn),
// MapUnit.RotationSpeed,
// ref turnSmoothVelocity,
// smoothingTime
// );
float smoothedTurn = Mathf.Lerp(Animator.GetFloat(Turn), Player.Data.RotationSpeed, Time.deltaTime * 10f);
Animator.SetFloat(Turn, smoothedTurn);
float layerWeight = Animator.GetLayerWeight(Animator.GetLayerIndex(Torso));
SetLayerWeight(Torso,
Mathf.MoveTowards(layerWeight, _isTorsoLayerEnabled ? 1f : 0f, Time.deltaTime * 3f));
}
public void SetLayerWeight(string layer, float weight)
{
Animator.SetLayerWeight(Animator.GetLayerIndex(layer), weight);
}
}
}