I'm trying to get it so my character will play a swing animation after being idle for 5 seconds. However, the timer is unable to get above 0.02 sec. I've tried a few methods that I've seen from other peoples questions but have been unable to get this to work. This is the current code I have for my 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);
}
}
}
As mentioned by @hijinxbassist the solution is to set float time = 0f
outside the Update function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.