How to add jump to this script for a 3d cube?

Hi everyone, I am super new to unity and can’t figure out how to add jumping to this script. If anyone could help that would be greatly appreciated. So far the script just makes the cube move forward back side to side but it is not able to jump. The character is just a simple green cube

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

public class playermove : MonoBehaviour {

public float moveSpeed;

// Use this for initialization
void Start () {
    moveSpeed = 10f;
}

// Update is called once per frame
void Update () {
   
    transform.Translate(moveSpeed*Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed*Input.GetAxis("Vertical") * Time.deltaTime);
}

}

First off, mind that transform.Translate will not check for any collisions, ever. So if you don’t want to have the Cube fall through floors as soon as you add gravity, you’d need to use a component that cares about collision (like a Rigidbody) or write custom collision code.

Since the question is about jumping only, let’s have a look at “only” jumping anyway.

What you currently have is movement based on a direction that is recalculated in every Frame. So whatever you did in the last Update doesn’t matter anymore in the next Update. This approach doesn’t work for gravity, since gravity is not a velocity, but an acceleration. This basically means that you cannot calculate the current movement without knowing the previous one, since the current movement is the previous movement accelerated downwards.

In code terms, this means that you have to remember your current movement (either in all directions or just the vertical speed) somehow; and use it to calculate the new movement in the next Update.

Here’s an example with only a float (and two for setting the gravity and jump power):

public float gravity = 9.81f;
public float jumpPower = 10f;
private float verticalSpeed = 0f;

private Update()
{
  var isGrounded = transform.position.y <= 0f; // A simple check to consider y = 0 as "the ground"
  if(isGrounded)
  {
    if(Input.GetButtonDown("Jump"))
    {
      verticalSpeed = jumpPower; // Set the vertical speed so next time it's used, we move upwards
    }
  }
  else // so if we're in the air and not grounded
  {
    verticalSpeed -= gravity * Time.deltaTime; // Accelerate downwards each frame
    // verticalSpeed will eventually go below zero with this, which means we start falling
  }

  if(verticalSpeed != 0f)
  {
    transform.Translate(Vector3.up * verticalSpeed * Time.deltaTime);
    
    // Some extra code for the example floor collision, not necessary if collision is checked otherwise
    if(transform.position.y <= 0f)
    {
      // If we accelerated *into* the ground, reset the position to 0, so we're *on* it
      var pos = transform.position;
      pos.y = 0f;
      transform.position = pos;
      
      // Stop vertical movement, starting next frame
      verticalSpeed = 0f;
    }
  }
}

Note: The two public values are public so you can set them in the editor without having to change code. The private variable verticalSpeed is private because its value is supposed to be controlled by the script, not the developer, so it doesn’t show up in the editor.

The whole stuff with isGrounded and transform.position.y is entirely so you don’t fall through the floor with this script. It’s very likely that for your idea, you want to get rid of it all sooner or later in order to use a proper collision detection solution - so here’s the actual gist for jumping in this code:

  1. If we’re on the ground and press jump: Set the vertical speed to a positive value.
  2. Move along the y Axis with the current vertical speed each frame
  3. Accelerate the vertical speed downwards each frame by subtracting a gravity value times Time.deltaTime
  4. If you touch the ground again, stop moving or accelerating downwards