Raycast vs OnCollisionStay in order to test for the ground drastically changes my jumping behavior

So I’ve been coding a character for a day or two now as one of my first proper unity ventures, and I’m having issues with the jumping. I opted to do it such a way that the player can only jump when he is not already jumping, and this is done by setting a jump variable to true when he jumps, and only setting it to false when he touches the floor again. While this is true, he cannot jump. EZMode.

The main key to the issue for me is getting the collision down in order to set the “jumping” variable to true. Initially I did this via an OnCollisionStay code, which can be seen in the code below. This worked pretty fantastic, only it meant that anything I wanted to be able to stand on and then jump off again (Crates for example) had to be marked as ground, and then I got funny behavior when I was standing against (and therefore colliding with) objects and holding the jump button.

I then moved onto raycast, and this also worked for the sake of changing the variable, however suddenly the jumping is drastically altered. Now I jump approx 10-20x higher, and I jump in such a way that just tapping the space bar is a tiny jump and holding it is a massive one. And also changing the jumps strength doesnt seem to change things and much as it did before.

My question really is twofold. Number 1, why on earth does something as simple as changing the way I’m detecting a collision make such a huge difference on the actual jumping? I’m having the player jump by increasing his velocity on the y axis, and to be perfectly honest I cannot see how a raycast would affect velocity. I’ve done a debug, and the “jumping” variable changes to true literally the instance I press the jump button (I thought perhaps the jump variable was staying false too long as a result of the raycast or something). Things I have tried: Placing the raycast in Update rather than FixedUpdate, placing it above and below the jumpcode, increasing and decreasing the distance of the raycast.

Number 2, is there a simpler way to do this so that I can stand and then jump off obstacles but not fly up them? Am I going about this all wrong?

Code below. The commented code works, but I edited it out for the raycast. If there are unusual formats/lines, its because I’ve been shifting it around so much.

using UnityEngine;
using System.Collections;

public class CharJumping : MonoBehaviour {



	public float jumpStr;
	private bool jumping;
	private Vector3 jumpVel;

	void FixedUpdate () 
	{

		Debug.Log (jumping);

		Vector3 jumpVel = new Vector3 (0, jumpStr, 0);

		if (Physics.Raycast (transform.position, Vector3.down, 1.0f)) 
		{
			jumping = false;
		}

		if (Input.GetAxis("Jump") !=0 && jumping == false) 
		{
			jumping = true;
			rigidbody.velocity = rigidbody.velocity + jumpVel;
		}
	}

	//void OnCollisionStay(Collision other) {
	//	if (other.gameObject.tag == "Ground") {
	//		jumping = false;
	//	}
	//}

}

Its because your ray is still hitting the ground after you leave it.
So, you are getting another “jump” when in mid air.

Your "jumping’ variable isn’t helping you because yo hare setting it false whenever the ray hits ground, which means whenever you are over the ground.

Rather then using a fixed 1.0f distance for your ray, you need to limit the ray to the height of your object, which would be the distance between your ray origin in world space and its render.bounds.min.y, assuming Y is up.