unity3d 如何正确播放三剑攻击动画?

flseospp  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(146)
  • 当我输入我的前3次攻击时,3个动画将只在我点击第4次后播放-当我点击垃圾邮件时,第1个动画将只在前3次垃圾邮件点击后播放,在3个动画完成后
    有什么帮助或建议吗?
public class swordscript : MonoBehaviour
 {
     public GameObject Sword;
     private Animator anim;
 
     private int comboStep = 0; // Current step in the combo
     private float lastComboTime = 0f; // Time when the last attack was started
     private float comboTimeout = 1.0f; // Time window for completing the combo
     private float ResetAtkCooldown = 0.5f;
 
     void Start()
     {
         // Get the Animator component from the Sword GameObject
         anim = Sword.GetComponent<Animator>();
     }
 
     void Update()
     {
         // Check if the player has clicked the mouse
         if (Input.GetMouseButtonDown(0))
         {
             
 
             // Check if we're in a combo
             if (Time.time - lastComboTime <= comboTimeout)
             {
 
                 // Increment the combo step
                 comboStep++;
 
                 // Perform the appropriate action based on the combo step
                 switch (comboStep)
                 {
                     case 1:
                         anim.SetTrigger("Attack");
                         StartCoroutine(ResetToIdle());
                         break;
                     case 2:
                         anim.SetTrigger("Attack2");
                         StartCoroutine(ResetToIdle());
                         break;
                     case 3:
                         anim.SetTrigger("Attack3");
                         StartCoroutine(ResetToIdle());
                         break;
                     default:
                         // Reset the combo if the player takes too long to complete it
                         if (comboStep > 3)
                         {
                             comboStep = 1;
                         }
                         break;
                 }
             }
             else
             {
                 // Start a new combo
                 comboStep = 1;
             }
 
             // Record the time of the last attack
             lastComboTime = Time.time;
         }
     }
     IEnumerator ResetToIdle()
     {
         yield return new WaitForSeconds(ResetAtkCooldown);
         comboStep = 0;
     }
 }

我希望动画攻击在我输入的每一次点击中播放(如第一次点击=攻击,第二次点击=攻击2,第三次点击=攻击2,并将在攻击字符串后再次重置,当我点击另外3次)

ycl3bljg

ycl3bljg1#

这段代码有很多问题,你最好把调试器放在外面,看看你的代码实际上是如何工作的,除了你预期的行为。

// Check if we're in a combo
if (Time.time - lastComboTime <= comboTimeout) {
    // Increment the combo step
    comboStep++;

1.因为,除非双击,否则Time. time-lastComboTime〈= comboTimeout总是返回false。所以,它不会通过单击跳转到组合控件块。
1.紧接着,连击步设置为1,增加到2.所以它从攻击2开始连击
对于这样的状态控制块,我推荐状态设计模式。我从这个很容易理解的博客中了解到:https://gameprogrammingpatterns.com/state.html
祝你好运

  • 注意:您可以散列触发字符串以获得更干净的代码和更好的性能 *
private static readonly int attack = Animator.StringToHash("Attack");

相关问题