This is frustrating

Dont understand what im doing wrong, I want my object to just simply jump whiles its moving towards the right by presing the spacebar and i want it so that the object jumps over other obstacles coming towards it. I want the player to use the spacebar to make the object jump over the obstacle basically. And i want the jump to be able to jump again. Not just one jump and thats it.

Right now i have this script and the objects moves towards the right yes, but when i press the spacebar for it to jump, it just falls down and off the screen and through my ground. I dont know what happened? is it a problem with gravity… or does something have to be appliedd…? like a layer or some type of component idk? is it something to do with gravity… dont understand im new and learning i just want to be able to understand whats going on.

Script:

using UnityEngine;
using System.Collections;
using System;

public class Frog : MonoBehaviour
{
private Vector3 movement;
private float ground;
private float speed = 10f;
private float jump_power = 15f;
public float gravity = 5f;

void Start()
{
    ground = transform.position.y;
}

void Update()
{
    movement.x = speed; // moving to the right
    if (transform.position.y == ground)
        movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
    else
        movement.y -= gravity; // gravity
    transform.Translate(movement * Time.deltaTime); // applies movement
}

}

if (transform.position.y == ground)
movement.y = (Input.GetButton(“Jump”) ? jump_power : 0f); // jump
else
movement.y -= gravity; // gravity

The only thing keeping the object from sinking is that you set movement.y to 0 if the object is at a right y-coordinate and ‘jump’ is not pressed.

You apply the movement to the object using Time.deltaTime which depends on your framerate.

So lets say you press jump and movement.y becomes 15 (jump_power). Then transform.Translate(movement * Time.deltaTime); changes the y position of the object to let’s say 0.94 (depends on deltatime). In the next Update()s the object drops by Time.deltaTime * gravity, let’s say first it drops by 0.31 then by 0.33 then by 0.31. Now the y position of the object is -0.01 and there isn’t anything to stop it from falling.

Somewhere you need to add a condition like if (transform.position.y < 0) transform.position = new Vector3(transform position.x, 0, transform position.z or make some other way to keep the object above ground.

In theory your code could work by using FixedUpdate instead of Update because that would make Time.deltaTime constant and because your jump_power is exactly 3*gravity, the object should mathematically land on y==0 height in some Update(). Unfortunately you can’t trust floating point numbers to be exactly what you expect. Y could still become -0.0000001 and break the system.

okay so what are you saying i should add? I added the fixed update and your code but nothing happened. the object still dropped when i press spacebar to make it jump. seems like it just moves to the right but once the spacebar is pressed , thats the end it just falls off screen