Question by
ikybeadie · Feb 07, 2019 at 07:01 AM ·
c#animation2dwaitforseconds
Help with changing bool over time to play animation
I have been having trouble lately trying to make a jump animation for my 2D game. Im new so please go easy on me. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DirectionAssist : MonoBehaviour {
private bool m_FacingRight = true;
public float Speed;
private float movement = 0f;
public Animator animator;
public float runSpeed = 40f;
bool IsGrounded = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
movement = Input.GetAxisRaw ("Horizontal") * runSpeed;
animator.SetFloat ("Speed", Mathf.Abs(movement));
if (IsGrounded == true)
animator.SetBool ("IsJumping", false);
if (IsGrounded == false)
animator.SetBool ("IsJumping", true);
//Disregard this for it is working
if (Input.GetKey (KeyCode.LeftArrow))
m_FacingRight = false;
//This too
if (Input.GetKey (KeyCode.RightArrow))
m_FacingRight = true;
}
//I think this may be the problem
void Foo() {StartCoroutine (Begin ());}
IEnumerator Begin() {
if (Input.GetButtonDown ("Jump"))
IsGrounded = false;
yield return new WaitForSecondsRealtime (2f);
IsGrounded = true;
}
}
Comment
Answer by xxmariofer · Feb 07, 2019 at 08:57 AM
change your code to this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DirectionAssist : MonoBehaviour {
private bool m_FacingRight = true;
public float Speed;
private float movement = 0f;
public Animator animator;
public float runSpeed = 40f;
bool IsGrounded = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
movement = Input.GetAxisRaw ("Horizontal") * runSpeed;
animator.SetFloat ("Speed", Mathf.Abs(movement));
if (IsGrounded == true)
animator.SetBool ("IsJumping", false);
if (IsGrounded == false)
animator.SetBool ("IsJumping", true);
//Disregard this for it is working
if (Input.GetKey (KeyCode.LeftArrow))
m_FacingRight = false;
//This too
if (Input.GetKey (KeyCode.RightArrow))
m_FacingRight = true;
if (Input.GetButtonDown ("Jump"))
StartCoroutine (Begin ());
}
IEnumerator Begin() {
IsGrounded = false;
yield return new WaitForSecondsRealtime (2f);
IsGrounded = true;
}
}