unity animator get bool

// unity animator get bool
//Fetch the Animator
Animator m_Animator;
// Use this to decide if the GameObject can jump or not
bool m_Jump;

void Start()
{
    //This gets the Animator, which should be attached to the GameObject you are intending to animate.
    m_Animator = gameObject.GetComponent<Animator>();
    // The GameObject cannot jump
    m_Jump = false;
}

void Update()
{
    //Press the space bar to enable the "Jump" parameter in the Animator Controller
    if (Input.GetKey(KeyCode.Space))
    {
        //Set the "Jump" parameter in the Animator Controller to true
        m_Animator.SetBool("Jump", true);
        //Check to see if the "Crouch" parameter is enabled
        if (m_Animator.GetBool("Crouch"))
        {
            //If the "Crouch" parameter is enabled, disable it as the Animation should no longer be crouching
            m_Animator.SetBool("Crouch", false);
        }
    }
    //Otherwise the "Jump" parameter should be false
    else m_Animator.SetBool("Jump", false);

    //Press the down arrow key to enable the "Crouch" parameter
    if (Input.GetKey(KeyCode.DownArrow))
        m_Animator.SetBool("Crouch", true);
    else
        m_Animator.SetBool("Crouch", false);
}

3.67
9
Ahawkins82 105 points

                                    // unity animator set bool
// Fetch the Animator
Animator m_Animator;
// Use this for deciding if the GameObject can jump or not
bool m_Jump;

void Start()
{
    //This gets the Animator, which should be attached to the GameObject you are intending to animate.
    m_Animator = gameObject.GetComponent&lt;Animator&gt;();
    // The GameObject cannot jump
    m_Jump = false;
}

void Update()
{
    //Click the mouse or tap the screen to change the animation
    if (Input.GetMouseButtonDown(0))
        m_Jump = true;

    //Otherwise the GameObject cannot jump.
    else m_Jump = false;

    //If the GameObject is not jumping, send that the Boolean &ldquo;Jump&rdquo; is false to the Animator. The jump animation does not play.
    if (m_Jump == false)
        m_Animator.SetBool(&quot;Jump&quot;, false);

    //The GameObject is jumping, so send the Boolean as enabled to the Animator. The jump animation plays.
    if (m_Jump == true)
        m_Animator.SetBool(&quot;Jump&quot;, true);
}

3.67 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source