簡體   English   中英

如何讓我的角色在閑置一段時間后播放 animation?

[英]How do I have my character play an animation after being idle for a certain amount of time?

我正在嘗試獲取它,以便我的角色在閑置 5 秒后播放 swing animation。 但是,計時器無法超過 0.02 秒。 我已經嘗試了一些我從其他人的問題中看到的方法,但一直無法使它起作用。 這是我的 animation state controller 的當前代碼:

public class AnimationStateController : MonoBehaviour
{
    Animator animator;
    int isWalkingHash;
    int isSprintingHash;
    int isJumpingHash;
    int isWalkingBackHash;
    int isIdleSwingHash;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        isWalkingHash = Animator.StringToHash("isWalking");
        isSprintingHash = Animator.StringToHash("isSprinting");
        isJumpingHash = Animator.StringToHash("isJumping");
        isWalkingBackHash = Animator.StringToHash("isWalkingBack");
        isIdleSwingHash = Animator.StringToHash("isIdleSwing");
    }

    // Update is called once per frame
    void Update()
    {
        bool isSprinting = animator.GetBool(isSprintingHash);
        bool isWalking = animator.GetBool(isWalkingHash);
        bool isJumping = animator.GetBool(isJumpingHash);
        bool isWalkingBack = animator.GetBool(isWalkingBackHash);
        bool isIdleSwing = animator.GetBool(isIdleSwingHash);

        bool walkPressed = Input.GetKey("w");
        bool sprintPressed = Input.GetKey("left shift");
        bool jumpPressed = Input.GetKey("space");
        bool walkBackPressed = Input.GetKey("s");

        float time = 0f;
        float threshold = 3f;
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")) {    
            time += Time.deltaTime;
            Debug.Log(time);
            if (time >= threshold) {
                animator.SetBool(isIdleSwingHash, true);
                time = 0f;
            }
        }

        // if player presses W key
        if (!isWalking && walkPressed) {
            animator.SetBool(isWalkingHash, true);
        }
        // if player is not pressing W key
        if (isWalking && !walkPressed) {
            animator.SetBool(isWalkingHash, false);
        }
        // if player is walking and presses left shift
        if (!isSprinting && (walkPressed && sprintPressed)) {
            animator.SetBool(isSprintingHash, true);
        }
        //if player stops running or stops walking
        if (isSprinting && (!walkPressed || !sprintPressed)) {
            animator.SetBool(isSprintingHash, false);
        }
        // if player presses space
        if (!isJumping && jumpPressed) {
            animator.SetBool(isJumpingHash, true);
        }
        // if player is not pressing space
        if (isJumping && !jumpPressed) {
            animator.SetBool(isJumpingHash, false);
        }
        // if player is pressing s
        if (!isWalkingBack && walkBackPressed) {
            animator.SetBool(isWalkingBackHash, true);
        }
        // if palyer is not pressing s
        if (isWalkingBack && !walkBackPressed) {
            animator.SetBool(isWalkingBackHash, false);
        }
    }
}

正如@hijinxbassist所提到的,解決方案是在更新 function 之外設置float time = 0f

暫無
暫無

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

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