簡體   English   中英

我在 Unity 中工作,我的 Punch 和 Jump 動畫沒有完全播放。 如何修復我的代碼?

[英]I am working in Unity and my Punch and jump animations don't play fully. How can I fix my code?

我試圖讓我的角色打拳和跳躍。 Jump 有效,但 animation 並不總是可以播放。 Punch 要么根本不播放,要么在 animation 中途凍結。 下面是我的代碼。 我認為錯誤出現在 StopPunch 周圍的行中(我沒有寫這些,但在類似問題的另一個解決方案中找到了)。 代碼是否正確,我的錯誤可能是統一的? 任何幫助將不勝感激!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playermovement : MonoBehaviour {

    public CharacterController2D controller;
    public Animator animator;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;
    bool punch = false;
  

    void Update()
    {
        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
        
        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
            animator.SetBool("IsJumping", true);
        }

        if (Input.GetButtonDown("Crouch"))
        {
            crouch = true;

        }else if (Input.GetButtonUp("Crouch"))
        {
            crouch = false;

        }
        if (Input.GetButtonDown("Punch"))
        {
            punch = true;
            if (punch)
            {
                animator.SetBool("IsPunching", true);
                StartCoroutine(StopPunch(2.0));
            }
        } 
            void StopPunch (float waitTime)
        {
                yield WaitForSeconds (waitTime);
                animator.SetBool("IsPunching", false);
            }
        
        
    }
    
    

    public void OnLanding ()
    {
        animator.SetBool("IsJumping", false);
    }

    public void OnCrouching (bool isCrouching)
    {
        animator.SetBool("IsCrouching", isCrouching);
    }
    void FixedUpdate ()
    {
        // Move our character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
        animator.SetBool("IsPunching", false);
       
    }
}

如果您單擊遠離打孔的過渡,在檢查器中您應該會看到屏幕截圖中的設置,這應該允許動畫完成。 我猜你需要使用其中一些設置。

Unity 動畫師過渡選項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM