Making my player jump while I have automatic forward movements

So I need a scrpt where my player can jump-pretty simple, right? Well, I cant seem to figure it to make it compatible with what else I got in the script.

using System.Collections; /*We are using the "using" keyword to signify that we are implementing the Collections class of the System package(because
unity can not use Generic as a baselayer for a script) in this scope/file*/
using System.Collections.Generic; /*We are using the "using" keyword to signify that we are implementing the Generic class of the System.Collections
package(because it contains generic class definition and provides you to better type safly) in this scope/file*/
using UnityEngine; /*We are using the "using" keyword to signify that we are implementing the UnityEngine class(because it allows us to use all of
unity class definitions, keywords, default method and everythine) in this scope/file*/

public class PlayerMovementE5 : MonoBehaviour /*Creates a public(which means it's accesible to every other file) with the name
"PlayerMovementE5" that imports it with the baseclass of "MonoBehaviour"(which means it can be attached to a gameobjct)*/
{

    public Rigidbody rb; /*Creates a public(which means it can be accesed by either the inspectoor-if thsscript is attached to a object-or to
    another script if that script imprts this file) using the Rigidbody keyword(which is a component that can be attached to a gameobject)
    nicknamed "rb"*/
     public Vector3 jump;
     Vector3 movement;

     public float minSpeed;
     public float maxSpeed;
     public float InputSpeed;
     public float AutomaticSpeed;
     public float JumpSpeed;
     float goFowards;

     public bool isGrounded;
    void Start() /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "Start" and...*/
    {
        //rb.useGravity = false; /*Sets the useGravity on the nicknamed rididdbody 'rb' to false(meaning the gameobject has no gravity)*/
        Debug.Log("I'm floating!! Wheee"); /*Logs a message to the console thatsays whatevers in the paranthesis*/
        jump = new Vector3(0.0f, 2.0f, 0.0f);
    }

    void OnCollisionStay() {
        isGrounded = true;
    }

    // Update is called once per frame
    void Update() /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "Update" and...*/
    {

        if(Input.GetKeyDown(KeyCode.Space) && isGrounded) {
            rb.AddForce(JumpSpeed * jump, ForceMode.Impulse);
            isGrounded = false;
        }

         float moveHorizontal = Input.GetAxisRaw("Horizontal");
         float moveVertical = Input.GetAxisRaw("Vertical");
         movement = new Vector3(moveHorizontal, 0, moveVertical);
         movement = movement * InputSpeed * Time.deltaTime;
    }

    public void FixedUpdate() { /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "FixedUpdate",
    and...*/
      rb.velocity = movement;//new Vector3(rb.velocity.x, rb.velocity.y, (Speed));
      //rb.velocity = new Vector3((Speed), rb.velocity.y, rb.velocity.z);

        goFowards = AutomaticSpeed * Time.deltaTime;
        goFowards = Mathf.Clamp(AutomaticSpeed, minSpeed, maxSpeed);
        rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, (goFowards));
    }
}

I have tried putting rb.velocity = new Vector3 in the if statement and movement = 0, JumpSpeed, 0 in the if statement

Is this for a FPS Controller, or 3D character?

3d character, and I tottaly forgot to mention the player is jumping rn but he is gliding downwards instead of jumping.

Line 54 above would wipe out your y motion… and why are you setting velocity again in 59, but only the .z coordinate?

Steps to success when you set rigidbody velocity:

  • only set the axes you want to change
  • always pass the existing velocity in for the ones you don’t want to change
  • only do it once per FixedUpdate() to keep things straight (do all the work in a temp Vector3)

Easiest way is to copy the velocity out to a temp Vector3, then do all the work in there, then put it back … once.

1 Like

If you wanna message me on discord we can team up to share knowledge in Unity, Blender and C#.
You can have all my scripts and props too.
Better to learn together right?