繁体   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