首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何让我的角色在空闲一定时间后播放动画?

我如何让我的角色在空闲一定时间后播放动画?
EN

Stack Overflow用户
提问于 2022-04-18 21:05:02
回答 1查看 199关注 0票数 0

我试图得到它,所以我的角色将发挥一个摇摆动画后,闲置了5秒。然而,计时器不能超过0.02秒。我尝试过一些我从其他人的问题中看到的方法,但一直无法让它发挥作用。这是我的动画状态控制器的当前代码:

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-18 21:12:30

正如@hijinxbassist所提到的,解决方案是在更新函数之外设置float time = 0f

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71917069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档