How can I get my sprite to jump?

I’m making a game for a class, and I want the player to move left and right using the arrow keys, and jump when pressing the up key (Mario style). It moves horizontally just fine, but I have no idea how to get it to jump as well. Here’s the script currently on my sprite:

using UnityEngine;
using System.Collections;

public class BoyController : MonoBehaviour {
	
	[HideInInspector]
	public bool jump = false;			
	
	public float moveForce = 365f;			
	public float maxSpeed = 5f;			
	public float jumpForce = 1000f;			
	
	private Transform groundDetector;		
	private bool grounded = false;			
	
	void Awake()
	{
		// Setting up references.
		groundDetector = transform.Find("GroundDetector");
	}
	
	void Update()
	{
		// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
		grounded = Physics2D.Linecast(transform.position, groundDetector.position, 1 << LayerMask.NameToLayer("Terrain"));  
		
		// If the jump button is pressed and the player is grounded then the player should jump.
		if(Input.GetButtonDown("Jump") && grounded)
			jump = true;
	}
	
	void FixedUpdate ()
	{
		// Cache the horizontal input.
		float h = Input.GetAxis("Horizontal");
		
		if(h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed) 			
			GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce); 		 		
		if(Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
			GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
		
		// If the player should jump...
		if(jump)
		{	
			GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
			jump = false;
		}
	}
}

Ok, you have 2 variants (i know only 2):

  1. Easier way but minus that your character will jump very fast:

GetComponent().AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);

  1. More hard to do but with tweakable jump time:

Make coroutine:

bool Jump_On;

IEnumerator Jump(float time_to_jump)
{
  Jump_On = true;
  while (Jump_On)
  {
    GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
    time_to_jump -= Time.fixedDeltaTime;
    if (time_to_jump <= 0)
    {
       Jump_On = false;
    }
    yield return new WaitForFixedUpdate();
  }
}

Then start it here:

void FixedUpdate ()
{
  if (jump && !Jump_On) // add !Jump_On to prevent start another jump when current in progress
         {   
             jump = false;
             Start_Coroutine(Jump(0.3f)); // 0.3 - time of jump in seconds (time of working force)
         }
}

Thanks, this worked for me!