another rigidbody jumping problem c#

what do i need to add to make the raycast’s collision with the ground enable/disable jumping? currently i can jump off a wall which is not wanted. i searched and found some discussion around this same subject and similar stuff but couldnt figure out a solution

pretty new to this, what ive done is basically copy some code here and there from tutorials, modify it a little and learn analyzing it and checking out some c# language tutorials, i do understand pretty much all the code ive posted here, just cant figure out how to use that raycast’s collision any further than console messages. and hours of trial and error trying all kinds of things with the code in question with slim results… would rock if anybody could throw some tips here, might help me realize some new stuff, having kind of a hard time writing own code still

raycast script:

using UnityEngine;
using System.Collections;

public class raycast : MonoBehaviour
{
	
	public float rayLength = 1f;
	
	
	
	public void Update ()
	{
		RaycastHit hit;
		if (Physics.Raycast(transform.position, -transform.up, out hit, rayLength))
		{
			if (hit.collider.gameObject.tag == "ground")
			{
				print ("xxx");
			}
		}
	
	}
}

characterControl script:

using UnityEngine;
using System.Collections;
 
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]


 
public class characterControls : MonoBehaviour
{
	
	public float turnSpeed = 90f;
	public float walkSpeed = 5f;
	public float runSpeed = 10f;
	
	protected bool running;

	public float gravity = 10.0f;
	public float maxVelocityChange = 10.0f;
	public bool canJump = true;
	public float jumpHeight = 2.0f;
	private bool grounded = false; 
	

	
	void Awake ()
	{
		rigidbody.freezeRotation = true;
		rigidbody.useGravity = false;
	}
 
	
	
	void FixedUpdate ()
	{
				
		transform.Rotate (0f, Input.GetAxis ("Mouse X") * turnSpeed * Time.deltaTime, 0f);
		
	    if (grounded)
		{
			Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
	        targetVelocity = transform.TransformDirection(targetVelocity);
	        targetVelocity *= walkSpeed;
			
			running = Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift);
			if (running) targetVelocity *= runSpeed;
			

			Vector3 velocity = rigidbody.velocity;
	        Vector3 velocityChange = (targetVelocity - velocity);
	        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
	        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
	        velocityChange.y = 0;
	        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
			
			
	        //jump
	        if (canJump  Input.GetKey(KeyCode.Space))
			{
	            rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
	        }			
	    }

		rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
 
	    grounded = false;
	}
 
	
	
	void OnCollisionStay ()
	{
		grounded = true;
	}
	
	
 
	float CalculateJumpVerticalSpeed ()
	{		
		return Mathf.Sqrt(2 * jumpHeight * gravity);
	}
}

first of all, your canJump variable is not set to false after jumping first time and is in mid-air or whatever. when you jump set your canJump to false:

  //jump
if (canJump  Input.GetKey(KeyCode.Space))
{
     rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
     canJump = false;
}

Then when the player touches the ground again set it to true.
and replace this:

void OnCollisionStay ()
    {
        grounded = true;
    }

with this

void OnCollisionEnter (Collision other)
{
    if(other.gameObject.layer == ground_layer)
    {
        grounded = true;
        canJump = true;
    }
}

thanks, it works a little different now, i’ll play around with it, any additional tips etc more than welcome