C# Jump (Z axis)

Making Jump script along Z axis

YouTube hasn’t helped

This is what i have so far…
Reference of current script

public int Gravity = 0;
public bool CanJump = true;
public CharacterController controller;
private Vector3 MoveDirection = Vector3.zero;
public int JumpHeight = 5;

if (CanJump  Input.GetKey ("space"))
		{
			MoveDirection.z = JumpHeight;
		}

		MoveDirection.z -= Gravity * Time.deltaTime;
		controller.Move (MoveDirection * Time.deltaTime);

I dont understand from line 8+ and its not working due to CharacterController
Please Explain code from line 8+ and state if anything is wrong.

This script is for a jump 2D top down view game, so the player must jump towards Z axis as Unity is set for 2D

I would recommend posting more of the script. You’re using a variable JumpHeight - but based on my skimming is never initialized.

Im trying to create the script to jump on the Z axis, the JumpHeight can be what ever number you want, i just want to create a working jump script to which i can tweek the values later.

Yes i forgot the Height, im sorry, will be updated soon. However for the time being it can be what ever number you want

Try Input.GetButtonDown(“Jump”)

instead of Input.GetKey (“space”)