Resetting my jump

Hello community,
I am doing a 2d sprite game and I am having issues with resetting my jump, I have used the 2d platformer tutorial for like my camera and level attributes but not the jumping because this is a 2d sprite game and I am using SM2, now the issue that I am having is reset the jump after I jump the first time, he lifts up in the air once and never wants to jump again, go figure but here is what I have for code.

if(isJumping)

		{

			print("Height reached");

			rigidbody.velocity = new Vector3(0f,5f,0f);

		}

Here is the jump for the rigidbody on the y axis
Then I call this method in update to make him jump:

//The space key is pressed and the player is on the

		//ground

		 if(Input.GetKey("space") isGrounded)

		{

                                   //handles the animation for the jump
                                    Jumping();

		    isJumping = true;

			
			isGrounded = false;

			reachedHeight = true;

		}

		else if(reachedHeight  !isGrounded)

		  {

			   isJumping = false;

		   }

The way that I have it set up is that if he has reached his height in jumping the portion of him being on ground is true and his velocity has been reset to zero so he could jump again, can’t see why this doesn’t work but may some fresh eyes could help.

Thanks

Having fun with code. I think your problem is that you need something telling your code that you are grounded and not jumping. If you are using a CharacterController, there is a different way to do this.

function OnCollisionStay(collisionInfo : Collision) {
isGrounded=true;
isJumping=false;
}


// concept time

// use a variable for the last vertical movement
var lastVertical=0.0;

function Update(){


if(lastVertical>0.0  rigidbody.velocity.y<=0.0){
//apex has been reached.
}
lastVertical=rigidbody.velocity.y;

}



// adding a doublejump with some twist
// use a variable for the last vertical movement
var lastVertical=0.0;
var jumpCount=0;
var jumpSpeed=5.0;

function Update(){

if(lastVertical>0.0  rigidbody.velocity.y<=0.0){
//apex has been reached.
}
lastVertical=rigidbody.velocity.y;

if(isJumping  jumpCount<2){
if(Input.GetKeyDown("space")){
jumpCount++;
rigidbody.velocity.y+=jumpSpeed;// to make the second jump slow down falling, or speed up jumping.
rigidbody.velocity.y=jumpSpeed; // to make the second jump as powerful as the first.
rigidbody.velocity.y=jumpSpeed - Mathf.Abs(rigidbody.velocity.y);// makes the jump only greatest at the apex
}
}

if(isGrounded  Input.GetKeyDown("space")){
isJumping=true;
rigidbody.velocity.y=jumpSpeed;// set the jump power
rigidbody.velocity.y+=jumpSpeed; // If you are going down a hill your jump wont be as powerful
rigidbody.velocity.y=jumpSpeed - Mathf.Abs(rigidbody.velocity.y);// jumps are strongest if you are standing still, or on a flat surface
jumpCount++;
}

}

function OnCollisionStay(collisionInfo : Collision) {
isGrounded=true;
isJumping=false;
jumpCount=0;
}

Alrigth this is what I am stuck on I am callinga raycast to check to see if I am on the ground like this.

// raycast check to see if the player is standing on the ground

        if (Physics.Raycast(thisTransform.position,new Vector3 (0f,-1f,0f),0.5f)); //new Vector3(0f,1f,0f), 10f))

		{

			isGrounded = true;

			isJumping = false;

		}

Now when I check to jump using your code I still undesired effects, so I guess I am asking is how do I make him jump and then reset his jump because with my code I got him to jump once but after that one time nothing else, I may just be missing something also who would you do it in C#?

There isn’t much that can logically go wrong with your code. One thing that might happen is if he didn’t move far enough after the first update where is jumping would still be false and stop everything. You may need a timer to make sure he has had enough time to lift off the ground before make isJumping false.

The other possibility is that the ray cast isn’t showing true when the character drops to the ground. In that case it would need to be longer. So you would need to test to see if the character is actually grounded after the first drop by using Debug.Log or something.

You are resetting isJumping and isGrounded, and those are your only tests, unless I’m missing something.

There could be a few errors in that script. If his center point was far enough off the ground that he was not ever on the ground. This would be a problem. Also, you start a jump start your landing and then declare a second jump before he hits the ground.

Omitting the Raycast and going with OnCollisionStay allows you to get past this as well it takes advantage of Unity’s natural flow: Update, Physics, LateUpdate, Render.

Try this basic code by it’s self. (providing there are no stupid errors)

var isGrounded=false;

funciton Update(){

// jump if we are on the ground
if(isGrounded  Input.GetKeyDown("space")){
rigidbody.velocity.y=10.0;
}

// late set of isGrounded. OnCollisionStay will over ride it if we are on something.
isGrounded=false;
}

function OnCollisionStay(collisionInfo : Collision) {
isGrounded=true;
}

First off I forgot to say thanks for the reply’s and the help, I didn’t mean to be rude because you guys are beast on here, second let me say that works great, now he automatically double jumps and is that what I have.

		if(Input.GetKeyDown("space")  Isgrounded )

		{

			rigidbody.velocity = new Vector3(0f,10f,0f);

			Isgrounded = false;

		}

Just like yours BigMisterB and then I called my collider and like Fireside said it was not calling my raycast all and I don’t have any idea why, I was trying to import some logic from my XNA version of the game and it didn’t work to well but I am gathering more about Unity the more I play with it.

Thanks guys.

One more question, how do you make him stop double jumping and when he hit his head on a collider if you continue to press jump he stays in the air, something that just happend and I didn’t change a thing, still needing some help thanks.