【问题标题】:Hold bool while animating动画时按住 bool
【发布时间】:2021-12-31 12:47:02
【问题描述】:

我是一名 3D 艺术家,我很难理解编程,但我正在努力。

一个多星期以来,我一直无法找到一个简单的解决方案来解决如何在播放攻击动画时冻结布尔值。由于我使用的是“更新”,因此按下“fire1”会在很短的时间内切换 bool,我需要它一直工作到攻击动画结束。

谢谢!

Animator

void Update () {
    
    // WASD
    
    float Horizontal = Input.GetAxis("Horizontal");
    float Vertical = Input.GetAxis("Vertical");
    
    Vector3 move = Quaternion.Euler (0, playerCamera.transform.eulerAngles.y, 0) * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    controller.Move(move * Time.deltaTime * playerSpeed);
    
    if (move != Vector3.zero)
    {
        gameObject.transform.forward = move;
        animator.SetBool("Walk", true);
    }
    else 
    {
        animator.SetBool("Walk", false);
        
    }
    
    // Attack
    
    if (Input.GetButtonDown("Fire1"))
    {
        playerSpeed = 0f;
        
    
        animator.SetBool("Attack", true);
        animator.SetBool("Walk", false);
        
        
    }
    else 
    {
        playerSpeed = 2f;
        animator.SetBool("Attack", false);
    }    
    }

【问题讨论】:

  • 为什么?你为什么不简单地配置你从Attack到Idle的转换,只在攻击动画完成后发生,而不依赖于Attack bool?
  • 我们一般Input.GetButtonDown 是true 仅在按下键的单个帧中.. 您的else 块在所有其他帧中执行并重置布尔值参数...
  • 动画按预期工作,重点是防止角色在攻击过程中移动。

标签: c# unity3d boolean animator


【解决方案1】:

正如您所理解的,这里发生的事情是 Input.GetkeyDown("Fire1") 在您按下按钮后单帧返回 true,然后变为 false,因此关闭了攻击动画。

我认为您可以使用动画制作器及其内置功能在设置的计时器后过渡到空闲/行走动画(我认为这就是您要寻找的)。像这样:The Attack Animation Transition after 1 second

空闲动画转换为攻击动画,攻击布尔:Idle Transition,步行动画也是如此

对于代码,由于您想在触发和移除攻击动画时更改玩家的速度,因此非常简单:

void Update () {

// WASD

float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");

Vector3 move = Quaternion.Euler (0, playerCamera.transform.eulerAngles.y, 0) * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);

if (move != Vector3.zero)
{
    gameObject.transform.forward = move;
    animator.SetBool("Walk", true);
}
else 
{
    animator.SetBool("Walk", false);
}

// Attack
if (Input.GetButtonDown("Fire1"))
{
    playerSpeed = 0f;
    animator.SetBool("Attack", true));
    //We don't need to set the walk bool off so that we directly transiton 
    //to the walk animation or the idle animation after  the attack
}
else
{
   animator.SetBool("Attack", false);
}

//if we're not on the attack animation, the animation 
//transitions by itself with the animator transitions
if(!animator.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
{
    playerSpeed = 2f;
}    
}

如果我的代码有误,或者您需要更多帮助,请告诉我。祝你好运!

【讨论】:

  • 问题不在于攻击的动画被打断,而是在攻击的动画过程中,角色可以移动,为此我需要 bool "Walk" 和 "Attaсk"。当玩家攻击,变量变为真,角色无法移动,但由于它在 1 帧内切换,因此无法正常工作。
  • 好吧,我确保当你触发攻击时,在Input.GetButtonDown("Fire1") 中,它会将你的 playerSpeed 设置为 0,当你的攻击动画停止时,这意味着 (!animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) 它会再次重新启动。您的攻击和步行布尔值是不必要的,因为动画师已经可以告诉您您在哪个状态下使用 animator.GetCurrenrAnimatorStateInfo(0).IsName("Name"),然后相应地改变您的速度。
  • 是的,它确实使代码更干净,但是如何防止玩家在攻击时移动呢?
猜你喜欢
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2016-05-02
相关资源
最近更新 更多