如何让我的角色在闲置一段时间后播放动画?
How do I have my character play an animation after being idle for a certain amount of time?
我正在尝试让我的角色在闲置 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);
}
}
}
如@hijinxbassist所述,解决方案是在更新函数
之外设置float time = 0f
我正在尝试让我的角色在闲置 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);
}
}
}
如@hijinxbassist所述,解决方案是在更新函数
之外设置float time = 0f