我试图得到它,所以我的角色将发挥一个摇摆动画后,闲置了5秒。然而,计时器不能超过0.02秒。我尝试过一些我从其他人的问题中看到的方法,但一直无法让它发挥作用。这是我的动画状态控制器的当前代码:
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);
}
}
}发布于 2022-04-18 21:12:30
正如@hijinxbassist所提到的,解决方案是在更新函数之外设置float time = 0f
https://stackoverflow.com/questions/71917069
复制相似问题