这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

44 行
1.5 KiB

using System;
using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
using Moment = UOP1.StateMachine.StateAction.SpecificMoment;
/// <summary>
/// Flexible StateActionSO for the StateMachine which allows to set any parameter on the Animator, in any moment of the state (OnStateEnter, OnStateExit, or each OnUpdate).
/// </summary>
[CreateAssetMenu(fileName = "AnimatorMoveSpeedAction", menuName = "State Machines/Actions/Set Animator Move Speed")]
public class AnimatorMoveSpeedActionSO : StateActionSO
{
public string parameterName = default;
protected override StateAction CreateAction() => new AnimatorMoveSpeedAction(Animator.StringToHash(parameterName));
}
public class AnimatorMoveSpeedAction : StateAction
{
//Component references
private Animator _animator;
private Protagonist _protagonist;
private AnimatorParameterActionSO _originSO => (AnimatorParameterActionSO)base.OriginSO; // The SO this StateAction spawned from
private int _parameterHash;
public AnimatorMoveSpeedAction(int parameterHash)
{
_parameterHash = parameterHash;
}
public override void Awake(StateMachine stateMachine)
{
_animator = stateMachine.GetComponent<Animator>();
_protagonist = stateMachine.GetComponent<Protagonist>();
}
public override void OnUpdate()
{
//TODO: do we like that we're using the magnitude here, per frame? Can this be done in a smarter way?
float normalisedSpeed = _protagonist.movementInput.magnitude;
_animator.SetFloat(_parameterHash, normalisedSpeed);
}
}