Can someone help me make a simple jump script?

Hello im just making a game that is VERY simple and im really really new to this. Im coding in c# and i want to make a jump script where the cube just stands still and jumps. I dont know what to write in the code. I tried to do somethings but it didnt work as intended. I guess you could use this to help me and maybe ill learn. I also havent set up to check if its touching the ground, i was just trying to find a way to make it jump first then ill do that.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	//Spawn Variables
	public float playerSetX;
	public float playerSetY;
	public float playerSetZ;


	//Movement Variables
	public float playerJumpHeight = 2;

	void Start () {

		//Player Spawn Point

		//This is where our player will start when the game is played.

		//player == game object. Game object == transform!
		transform.position = new Vector3(playerSetX, playerSetY, playerSetZ);
	}

	void Update () {

		//player to move up jumping

		 //player (gameobject) aka transform to move when i press the arrow keys or keyboard keys

		//jump
		if (Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.D) || Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow)) {
		
			transform.position += new Vector3(playerSetX, playerJumpHeight, playerSetZ);
			}
	}

}

You might want to check some tutorial at http://unity3d.com/learn

using UnityEngine;
using System.Collections;
    
    [RequireComponent(typeof(Rigidbody))]
    public class PlayerController : MonoBehaviour {
    
    	public Vector3 jump;
    	public float jumpForce = 2.0f;
    
    	public bool isGrounded;
    	Rigidbody rb;
    	void Start(){
    		rb = GetComponent<Rigidbody>();
    		jump = new Vector3(0.0f, 2.0f, 0.0f);
    	}
    
    	void OnCollisionStay()
    	{
    		isGrounded = true;
    	}
    
    	void Update(){
    		if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
    
    			rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    			isGrounded = false;
    		}
    	}
    }

I know I’m three years late but here’s my two cents - the doyble jump is annoying but there’s a simple fix:

 using UnityEngine;
 using System.Collections;
     
     public class PlayerController : MonoBehaviour {
     
         public Vector3 jump;
         public float jumpForce = 2.0f;
     
         public bool isGrounded;
         Rigidbody rb;
         void Start(){
             rb = GetComponent<Rigidbody>();
             jump = new Vector3(0.0f, 2.0f, 0.0f);
         }
     
         void OnCollisionStay()
         {
             isGrounded = true;
         }
         void OnCollisionExit(){
           isGrounded = false;
     }
         void Update(){
             if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
     
                 rb.AddForce(jump * jumpForce, ForceMode.Impulse);
                 isGrounded = false;
             }
         }
     }

This means that, when you leave the ground, isGrounded is set to false. I would also suggest a layers system, otherwise you can wall-jump.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Youjump : MonoBehaviour
{
	public float speed;

    // Start is called before the first frame update
    void Start()
    {
		
    }

    // Update is called once per frame
    void Update()
    {

     // Jump is an axis predefined in the Unity input manager
     // go to edit->Project Settings->Input and Check
		  transform.Translate(0,speed*Input.GetAxis("Jump")*Time.deltaTime,0);

    }
}

using UnityEngine;
using System.Collections;

      public class PlayerController : MonoBehaviour {
      
          public Vector3 jump;
          public float jumpForce = 2.0f;
      
          public bool isGrounded;
          Rigidbody rb;
          void Start(){
              rb = GetComponent<Rigidbody>();
              jump = new Vector3(0.0f, 2.0f, 0.0f);
          }
      
          void OnCollisionStay()
          {
              isGrounded = true;
          }
          void OnCollisionExit(){
            isGrounded = false;
      }
          void FixedUpdate(){
              if(Input.GetKey("space") && isGrounded){
      
                  rb.AddForce(jump * jumpForce *Time.deltaTime, ForceMode.Impulse);
                  isGrounded = false;
              }
          }
      }

I know this post was made forever ago but just in case anyone else is looking at this in the future I found the above code to be extremely inconsistent on when it would detect my jump and also how high I would end up jumping so this is what I changed it too and it seems to be working for me so far. Just so you know you will have to increase the jump force a lot compared to what you had it set at.

@Arcane-Potato , I used your script to great success, I was able to get my player to move AND jump. It doesn’t double-jump. The height of the jump can be very inconsistent though. I’m extremely new, so I was trying to figure out why. Since ‘jump’ and ‘jumpForce’ were set values, I thought it might be the Time.deltaTime, but when I removed that the player would jump sky high and never come back down. Any thoughts as to why the height of the jump is inconsistent?

I know this question has been fully answered, but I have a solution that manages the jump curve and gives you that less floaty feel.


Visit my GitHub for the code: GitHub - I-Am-Err00r/Total-Jump-Solution: The scripts for the jump solution based on the YouTube video I uploaded


If you want an explanation on how this works, visit my YouTube video explaining this solution: Total Jump Solution: Less Floaty Jumps While Managing Jump Height Curve For 2D And 3D Platformers - YouTube

I use all method but it is not working!