Trying to create a jump script without velocity

I’ve done literally weeks of homework on this issue with no luck. I need to create a jump without using Unity’s rigidbody physics. I can make a jump with transform.translate well enough, but not one that looks and works well for character movement. Anyone have a solve to create a good jump mechanic without Using unity’s physics engine?

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

public class movement : MonoBehaviour
{

[SerializeField] float throttle = 6f; // pawn speed
[SerializeField] Rigidbody rb;
[SerializeField] Vector3 pStop;
[SerializeField] Vector3 pStart;
[SerializeField] float rotspeed = 100f;

public bool isGrounded = true;

void Start()
{
    rb = GetComponent<Rigidbody>();
    
}

void Update()
{
    // **** W,Q,S,E ******************************************************************* 
    if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0)
    {
        transform.position += transform.forward * throttle * Time.deltaTime * Input.GetAxis("Vertical");
    }
    if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
    {
        transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
    }
    // **** END W,Q,S,E ***************************************************************

    // **** A,D ***********************************************************************
    float turn = rotspeed * Time.deltaTime * Input.GetAxisRaw("Turn");
    transform.Rotate(0, turn, 0);
    // **** END A,D *******************************************************************

    // **** IF GROUNDED ***************************************************************
    //reset jump
    if (Physics.Raycast(transform.position, new Vector3(0, -1, 0), 1.35f))
    {
        isGrounded = true;
    }
    //make sure jump isn't possible while no ground
    else
    {
        isGrounded = false;
    }
    // **** END IF GROUNDED ***********************************************************

   
    

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
    {
        //i += Time.deltaTime;
        //transform.position = Vector3.Lerp(pStart, pStop, i);
        //transform.position = transform.up * Time.deltaTime;

        pStart = transform.position;
        pStop = transform.position + new Vector3(0, 5f, 0);
        float t = 0f;
        while (t < 1)
        {
            t += Time.deltaTime;
            transform.position = Vector3.Lerp(pStart, pStop, t);
        }
    }   

}

}

What you have inside your jump key if-body doesn’t make much sense. You essentially need those things:

  • gravity
  • y velocity

Something like this:

float yVel = 0; // keep track of your current yVelocity
float gravity = 9.81; // gravity acceleration

void Update()
{
   // [ ... ]
    if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
    {
        yVel = 5; // jump impulse force
        isGrounded = false;
    }
    if (isGrounded)
    {
        yVel = 0;
    }
    else
    {
        yVel -= gravity * Time.deltaTime;
    }
    transform.position += transform.right * yVel * Time.deltaTime;
}

Note that since we now move up and down from the ground you probably should adjust the y position when you “land” (when you set isGrounded). The raycast will give you the distance to the ground so once your ray hits the ground you probably want to set your character to a certain distance from the ground so the ray still intersects. Otherwise you may “land” on slightly different y positions. This is just the “straight-forward” implementation.

Since gravity depends on the time squared it’s actually not framerate independent. For more information see this question or this

Better Jumping in Unity With Four Lines of Code - YouTube might be of some use. He is using physics, but the concepts he goes over explains why games like Super Mario Bros. have those jumps that just feel right. Basically make the player fall faster than they should. Why the aversion to Unity’s physics?

Although K-ANator’s answer doesn’t address my original problem, he solved my bigger problem. Which was more important. My original code was causing erratic jump behavior due to a misplaced ‘=’ sign I believe. I am no longer experiencing the problem with my jump and I am very, very thankful.