好的,下面是代码:
// Variable to store character animator component
Animator animator;
// Variables to store optimized setter/getter parameter IDs
int isWalkingHash;
int isRunningHash;
// Variable to store the instance of the PlayerInput
PlayerInput input;
// Variables to store player input values
Vector2 currentMovement;
bool movementPressed;
bool runPressed;
// Awake is called when the script instance is being loaded
void Awake()
{
input = new PlayerInput();
// Set the player input values using listeners
input.CharacterControls.Movement.performed += ctx => {
currentMovement = ctx.ReadValue<Vector2>();
movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
};
input.CharacterControls.Run.performed += ctx => runPressed = ctx.ReadValueAsButton();
}
// Start is called before the first frame update
void Start()
{
// Set the animator reference
animator = GetComponent<Animator>();
// Set ID references
isWalkingHash = Animator.StringToHash("isWalking");
isRunningHash = Animator.StringToHash("isRunning");
}
// Update is called once per frame
void Update()
{
handleMovement();
}
void handleMovement()
{
// Get parameter values from animator
bool isRunning = animator.GetBool(isRunningHash);
bool isWalking = animator.GetBool(isWalkingHash);
// Start walking if movement pressed is true and not already walking
if (movementPressed && !isWalking) {
animator.SetBool(isWalkingHash, true);
}
// Stop walking if movementPressed is false and not already walking
if (!movementPressed && isWalking) {
animator.SetBool(isWalkingHash, false);
}
// Start running if movement pressed and run pressed is true and not already running
if ((movementPressed && runPressed) && isRunning) {
animator.SetBool(isRunningHash, true);
}
// Stop running if movement pressed or run pressed is false and not currently running
if ((!movementPressed || !runPressed) && isRunning) {
animator.SetBool(isRunningHash, false);
}
}
void OnEnable()
{
//Enable the character control action map
input.CharacterControls.Enable();
}
void OnDisable()
{
//Disable the character control action map
input.CharacterControls.Disable();
} 我一开始使用的是UnityEngine.InputSystem。但是移动效果很好,就在我松开WASD的时候,它仍然在最后一个按下的方向上移动。我真的不知道这里有什么问题。我刚刚开始,所以我不知道最简单的问题的答案。很抱歉打扰你。提前感谢!
发布于 2021-09-26 13:38:31
在下面的代码块中
// Stop walking if movementPressed is false and not already walking
if (!movementPressed && isWalking) {
animator.SetBool(isWalkingHash, false);
}你说你正在检查“还没有走”。它不会是'!isWalking‘作为if条件吗?
https://stackoverflow.com/questions/69335066
复制相似问题