Strange happenings with my code

So im trying to make it so that if my player is above a terrain sample with a new height than the one he jumped on, he wont stop in midair. Here’s my code, its somewhere in the jump controls. what do i change to make this possible?

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public float Speed;
	public float JumpHeight;
	public float JumpSpeed;
	public bool Jumping;
	public float PlayerFoot;
	public float PlayerFootSet;
	public float TerrainHeight;
	public float TerrainHeightSet;

	public float Sensitivity;
	private float MouseXSet;

	void Start () {
		MouseXSet = 0;
		Jumping = false;
		transform.position = new Vector3 (0, TerrainHeight + transform.lossyScale.y / 2, 0);
	}

	void Update () {
		PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
		TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);

		// Translation controls]
		if (Input.GetKey (KeyCode.W))
		{
			transform.position += transform.forward * Speed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.S))
		{
			transform.position -= transform.forward * Speed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.D))
		{
			transform.position += transform.right * Speed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.A))
		{
			transform.position -= transform.right * Speed * Time.deltaTime;
		}

		// Rotation controls
		if (Input.mousePosition.x > MouseXSet)
		{
			transform.Rotate (new Vector3 (0, 1, 0) * (Input.mousePosition.x - MouseXSet) * Sensitivity * Time.deltaTime);
			MouseXSet = Input.mousePosition.x;
		}
		if (Input.mousePosition.x < MouseXSet)
		{
			transform.Rotate (new Vector3 (0, -1, 0) * (-(Input.mousePosition.x - MouseXSet)) * Sensitivity * Time.deltaTime);
			MouseXSet = Input.mousePosition.x;
		}

		// Jump controls
		if (Input.GetKey (KeyCode.Space) && Jumping == false && PlayerFoot < TerrainHeight + 1) 
		{
			Jumping = true;
			TerrainHeightSet = TerrainHeight;
		}
		if (Input.GetKeyUp (KeyCode.Space))
		{
			Jumping = false;
		}
		if (Jumping == true)
		{
			if (PlayerFoot < TerrainHeightSet + JumpHeight * 10)
			{
				PlayerFootSet = PlayerFoot;
				rigidbody.velocity = new Vector3 (0, (JumpSpeed * 10 / (TerrainHeightSet + JumpHeight * 10 - PlayerFoot)) * JumpSpeed * 3, 0);
			} else {
				Jumping = false;
			}
		}
		if (Jumping == false)
		{
			if (PlayerFoot > TerrainHeight)
			{
				Debug.Log ("Here");
				if (PlayerFootSet < TerrainHeight + JumpHeight * 10)
				{
					Debug.Log ("There");
					rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * JumpSpeed, 0);
				}
			} else {
				rigidbody.velocity = new Vector3 (0, 0, 0);
			}
		}

		// Keep player upright
		if (PlayerFoot < TerrainHeight)
		{
			transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
		}
	}
}

Record the start height (when space is first pressed) and set the maximum jump height to startHeight + jumpHeight. Let that control how high your character can jump rather than the distance from the ground.

Use the terrain height to detect when the jump ends.

If Space is pressed and Jumping == True the you should do nothing handle the jump code in a jumping function and check if(Jumping) in Update to call that function if Jumping == True.

Looks like you’re OK at coding but give me a nudge if you want a hand implementing.

If you code like this then even if your character jumps off a cliff they should still fall as they only ever get up to startHeight + jumpHeight.

EDIT:

Well you’re setting Jumping to false when you let go of the button. That isn’t really true is, the character is still jumping until they land.

It depends on how you want the game to work, do you want the character to jump to the maximum height no matter how long space is pressed or do you want to only keep going up if space is being pressed?

That’ll affect how we deal with the code.