55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using UnityEngine;
|
|
using Fusion;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class PlayerAnimationHandler : NetworkBehaviour
|
|
{
|
|
[Header("Animator Settings")]
|
|
[SerializeField] private string speedParamName = "Speed";
|
|
[SerializeField] private string velocityXParamName = "Velocity X";
|
|
[SerializeField] private string velocityZParamName = "Velocity Z";
|
|
[SerializeField] private float animationDamping = 0.2f;
|
|
|
|
private Animator anim;
|
|
private int speedHash;
|
|
private int velocityXHash;
|
|
private int velocityZHash;
|
|
private bool hasSpeedParam;
|
|
private bool hasVelocityXParam;
|
|
private bool hasVelocityZParam;
|
|
|
|
public void Initialize(Animator animator)
|
|
{
|
|
this.anim = animator;
|
|
if (anim != null)
|
|
{
|
|
foreach (AnimatorControllerParameter param in anim.parameters)
|
|
{
|
|
if (param.name == speedParamName) hasSpeedParam = true;
|
|
if (param.name == velocityXParamName) hasVelocityXParam = true;
|
|
if (param.name == velocityZParamName) hasVelocityZParam = true;
|
|
}
|
|
}
|
|
|
|
speedHash = Animator.StringToHash(speedParamName);
|
|
velocityXHash = Animator.StringToHash(velocityXParamName);
|
|
velocityZHash = Animator.StringToHash(velocityZParamName);
|
|
}
|
|
|
|
public void UpdateAnimator(float speed, Vector2 moveInput, float deltaTime)
|
|
{
|
|
if (anim == null) return;
|
|
|
|
if (hasSpeedParam) anim.SetFloat(speedHash, speed, animationDamping, deltaTime);
|
|
if (hasVelocityXParam) anim.SetFloat(velocityXHash, moveInput.x * speed, animationDamping, deltaTime);
|
|
if (hasVelocityZParam) anim.SetFloat(velocityZHash, moveInput.y * speed, animationDamping, deltaTime);
|
|
}
|
|
|
|
public void SetSpeed(float speed)
|
|
{
|
|
if (anim != null && hasSpeedParam) anim.SetFloat(speedHash, speed);
|
|
}
|
|
}
|
|
}
|