如何统一使用新的输入系统进行攻击



我正在制作一款团结一致的游戏,并试图在每个方向(上、下、左和右(使用不同的动画进行有效的攻击。目前,我只是想让动画发挥作用,但我不知道如何根据用户的输入更改动画师布尔变量,在这种情况下是OnFire消息。我找到了一些教程,介绍了如何对单个攻击方向执行此操作,以及其他通过翻转攻击动画来执行此操作的教程,但我想使用我的所有4个动画来确定攻击方向。

我会在下面附上我目前的剧本,如果有人能帮忙的话,那就太好了。(这是一个糟糕的描述,所以人们有任何问题我都会回答(

private void FixedUpdate() //This function is called a fixed number of times each second
{
//This is all script for the movement and animation of the player
if(movementInput != Vector2.zero)
{
bool success = TryMove(movementInput);
//If it is not possible to move in both directions at the same time
if (!success)
{
success = TryMove(new Vector2(movementInput.x, 0)); //Try to move in the x direction
if (success)
{
success = TryMove(new Vector2(0, movementInput.y)); //Try to move in the y direction
}
}
if(movementInput.x < 0) //If the player is moving left
{
animator.SetBool("IsMovingLeft", true); //The Boolean variable for moving left is the only thing set to true
animator.SetBool("IsMovingDown", false);
animator.SetBool("IsMovingRight", false);
animator.SetBool("IsMovingUp", false);
}
else if(movementInput.x > 0) //If the player is moving right
{
animator.SetBool("IsMovingLeft", false);
animator.SetBool("IsMovingDown", false);
animator.SetBool("IsMovingRight", true); //The Boolean variable for moving right is the only thing set to true
animator.SetBool("IsMovingUp", false);
}
else if(movementInput.y < 0) //If the player is moving down
{
animator.SetBool("IsMovingLeft", false);
animator.SetBool("IsMovingDown", true); //The Boolean variable for moving down is the only thing set to true
animator.SetBool("IsMovingRight", false);
animator.SetBool("IsMovingUp", false);
}
else if(movementInput.y > 0) //If the player is moving up
{
animator.SetBool("IsMovingLeft", false);
animator.SetBool("IsMovingDown", false);
animator.SetBool("IsMovingRight", false);
animator.SetBool("IsMovingUp", true); //The Boolean variable for moving up is the only thing set to true
}
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
}
private bool TryMove(Vector2 direction)
{
//Check for potential collisions
int count = rb.Cast(
direction, //X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, //The settings that determine where a collision can occur on, such as layers to collide with
castCollisions, //List of collisions to store the found collisions into after the cast is finished
moveSpeed * Time.fixedDeltaTime + collisionOffset);
if (count == 0)
{
rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime); //Moving the game object
return true;
}
else
{
return false;
}
}
void OnMove(InputValue movementValue)
{
movementInput = movementValue.Get<Vector2>();
}
void OnFire(InputValue )
{
//Difficulty is here (I think)
}

我已经解决了这个问题,原来我的问题是使用布尔变量在状态之间转换,而更容易使用触发器作为条件。所以我启动攻击动画的最后代码是这样的:攻击动画脚本图像

最新更新