Limit Jumping and Play A Sound

I have a 2D ball rolling game, and I have the ball rolling left and right, and jumping. But you can hold down jump and fly. How would I make it so you can only jump while touching the ground, and to play a sound when you land? Thanks.

When jump is triggered via button or something, if m_onFly is false set boolean m_onFly to true and jump. If m_onFly was true, do nothing.

When code comes to OnCollisionEnter method and tag or name is indicatimg hitting to terrain set m_onFly to false if it was true and trigger sound effect. That’s about it.

So do I just put those things in where you said? Sorry, new to programming and learning as I go along. Here is my jumping script:

if (Input.GetAxis(“Jump”)) {
rigidbody.AddForce(Vector3.up * 70);
}

// This is somewhere in your method.
if (Input.GetAxis("Jump")) 
    {
    if ( !m_onFly )
        {
        m_onFly = true;
        rigidbody.AddForce(Vector3.up * 70);
        }
    }

/**
* Enters when colliding to something.
*/
void OnCollisionEnter( Collision p_collider )
    {

    if ( p_collider.tag == "TerrainOrWhatEverTheTerrainTagOrNameIsHowCanIKnow"  m_onFly )
        {
        m_onFly = false;
        if ( m_audio != null ) m_audio.PlayThatAudio();
        }
    }

Not tested, but that’s what popped up into my mind first.

I put this in just to get the jumping working for starters:

I get this error:

This was just a code snippet - you’ll have to declare the variable yourself at the top of the script.

Hmm, I really need to learn more about coding, but I just want to make my game :(. And after a couple more things, it won’t need that much coding.

Ok, here is what I have. It still flies, help?

http://pastie.org/745499

Anyone? I really want this to work, and have given up on making it until it does :frowning:

you should have inside that scrip code that I placed earlier.

After that modify this line:

 if (Input.GetAxis("Jump"))

to look like this, now m_onFly is taken care during jump time.

 if (Input.GetAxis("Jump")  !m_onFly )

under this line:

var gravitypull = 0;

add this line

var m_onFly = false;

Also, please make sure that your terrain/ground or what ever name/tag is correctly spelled inside OnCollisionEnter() and if you’re using tag it’s defined correctly to that terrain collider.

I think that you are using this grounded to indicate similarly information that I’m looking with this m_onFly.

Yeah, someone told me to use Grounded instead. I am using rectangles for my game, as it’s a 2D platformer, will I have to name them all the same thing so that it works properly?

This is what it looks like now, is this right?

http://pastie.org/746982

Oh, this is fun :smile:.

You can use that grounded variable if it’s correctly setted.

This my example is using same idea, but other way around.

  if (Input.GetAxis("Jump")  !m_onFly )
        {
        if ( m_onFly )
        {
        m_onFly = true;
        rigidbody.AddForce(Vector3.up * 70);    }
        }

Now if you have code like this, what happens when execution, I’ll try to pseudo it to you:

If User input tells us to jump and we are not on air yet go inside this if
     If we are flying already then go inside this if
          Set us to FLY!!!!

So it’s not working.

  if (Input.GetAxis("Jump")  !m_onFly )
     {   
        m_onFly = true;
        rigidbody.AddForce(Vector3.up * 70);
     }

This drop in pseudo:

If User input tells us to jump and we are not on air yet go inside this if
          Set us to FLY!!!!

Ok, but my character is still flying?

Oh, crap, so you mean that it won’t never land? If that’s the case, try to add rigidbody to that ball gameobject.

I red your posts too quickly, because I thought that it jumps and lands, but if you push several times jump, then it “re-jumps” while it’s on air. At least that’s how I understand this “But you can hold down jump and fly.”

No, it lands, but I can keep tapping it to jump and hold it to fly. I just want it to be like most platformers, press Space to jump, and maybe hold to go a bit higher.

using UnityEngine;

/**
* Test class
*/
public class move : MonoBehaviour 
	{
	// Am I jumping already	
	bool m_onFly = false;
		
	/**
	* FixedUpdate	
	*/		
	void FixedUpdate()
			{	
			if (0 < Input.GetAxis("Jump") )
				{
				Debug.Log("Jump");	
				if ( !m_onFly )
					{
					m_onFly = true;
					rigidbody.AddForce(Vector3.up * 1000f);
					}
				}	
			}
		
	/**
	* OnCollisionEnter
	*/
	void OnCollisionEnter( Collision p_collider )
		{

		if ( p_collider.transform.tag == "base"  m_onFly )
			{
			m_onFly = false;
//			if ( m_audio != null ) m_audio.PlayThatAudio();
			}
		} 
	}

Here’s code which worked fine on my computer and idea comes as it is without grounds etc stuff. store this as move.cs and test it.

var m_onFly = Physics.Raycast
        (transform.localPosition, transform.TransformDirection  
        (Vector3.down), 1.15);

I didn’t note this before, this is what is messing with my idea. Surely you can use this sort of implementation to detect ground as well as with collision :smile:, but then would be good idea to ignore player layer and there is some other problems in that call as well, but that’s not issue in this post.[/code]

It’s all so confusing :(. There’s different code everywhere.