Bool parameter stuck

EDIT: I got it to work. I’m not sure what the problem was. Maybe the conditions for the transitions.

Hello everyone,

I’m new to unity and programming in general and would be very grateful if anybody could help.
I successfully created a cube that moves and (charged) jumps only if in contact with the ground (onground=true).
Then I created 3 clips for the cube:
-while idle,
-charging the jump while button is pressed
-and the jump, when the button is released.

If I add this animation the bool parameter onground=true gets stuck 50% of the time. It depends on how long I press the button.

using UnityEngine;
using System.Collections;

public class jump_forward : MonoBehaviour {

    private Rigidbody rb;
    private float jumpcounter = 0f;
    private float height = 10f;
    private float maxheight = 30f;
    private float minheight = 30f;
    private float minforward = -1f;
    public bool onground;
    private Animator anim;


    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<Animator> ();
    }


    void FixedUpdate ()
    {
        //count jumpcounter up
        if (onground  & Input.GetButton ("Jump"))
        {          
            jumpcounter += 1f;
            anim.SetFloat ("jumppressure", jumpcounter);

            //jump if jumpcounter>=maxheight
            if (jumpcounter >= maxheight)
            {  
                //create maximum jump height
                Vector3 maxjump = new Vector3 (0f, jumpcounter + minheight, 0f);
                //create forward movement
                Vector3 maxforwardjump = jumpcounter * minforward * transform.forward + maxjump;
                //maxjump in direction cube is facing
                rb.AddForce (maxforwardjump * height);
                onground = false;
                jumpcounter = 0f;
                anim.SetFloat ("jumppressure", 0f);
                anim.SetBool ("onground", onground);

            }
        }

        //jump if user stops pressing space bar
        if (onground & Input.GetButtonUp ("Jump"))
        {
            //set minimum jump height
            Vector3 jumpheight = new Vector3 (0f, jumpcounter + minheight, 0f);
            //set forward movement
            Vector3 forwardjump = jumpcounter * minforward * transform.forward + jumpheight;
            //jump in direction cube is facing
            rb.AddForce (forwardjump * height);
            onground = false;
            jumpcounter = 0f;
            anim.SetFloat ("jumppressure", 0f);
            anim.SetBool ("onground", onground);
            //Debug.Log (onground);
        }          

    }

    //if cube hits floor onground=true
    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.CompareTag("Floor"))
        {
            onground = true;
            anim.SetBool ("onground", onground);
        }
    }
}

How do I get onground=false during every jump?

you have onground = false within a statement that checks to see if the ground is false.

try this

                 onground = false;
  //jump if user stops pressing space bar
        if (onground & Input.GetButtonUp ("Jump"))
        {
            //set minimum jump height
            Vector3 jumpheight = new Vector3 (0f, jumpcounter + minheight, 0f);
            //set forward movement
            Vector3 forwardjump = jumpcounter * minforward * transform.forward + jumpheight;
            //jump in direction cube is facing
            rb.AddForce (forwardjump * height);
            jumpcounter = 0f;
            anim.SetFloat ("jumppressure", 0f);
            anim.SetBool ("onground", onground);
            //Debug.Log (onground);
        }

this way, the bool is set to false unless the cube is in contact with the ground.

I would suggest checking to see if the object is NOT touching the ground though, because the object would more than likely be on the ground more often than in the air. :slight_smile:

hope this helps.