Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Making my player jump while I have automatic forward movements

Discussion in 'Scripting' started by RealDevMashup, Oct 30, 2021.

  1. RealDevMashup

    RealDevMashup

    Joined:
    Oct 5, 2021
    Posts:
    8
    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.
    Code (CSharp):
    1. using System.Collections; /*We are using the "using" keyword to signify that we are implementing the Collections class of the System package(because
    2. unity can not use Generic as a baselayer for a script) in this scope/file*/
    3. using System.Collections.Generic; /*We are using the "using" keyword to signify that we are implementing the Generic class of the System.Collections
    4. package(because it contains generic class definition and provides you to better type safly) in this scope/file*/
    5. 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
    6. unity class definitions, keywords, default method and everythine) in this scope/file*/
    7.  
    8. public class PlayerMovementE5 : MonoBehaviour /*Creates a public(which means it's accesible to every other file) with the name
    9. "PlayerMovementE5" that imports it with the baseclass of "MonoBehaviour"(which means it can be attached to a gameobjct)*/
    10. {
    11.  
    12.     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
    13.     another script if that script imprts this file) using the Rigidbody keyword(which is a component that can be attached to a gameobject)
    14.     nicknamed "rb"*/
    15.      public Vector3 jump;
    16.      Vector3 movement;
    17.  
    18.      public float minSpeed;
    19.      public float maxSpeed;
    20.      public float InputSpeed;
    21.      public float AutomaticSpeed;
    22.      public float JumpSpeed;
    23.      float goFowards;
    24.  
    25.      public bool isGrounded;
    26.     void Start() /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "Start" and...*/
    27.     {
    28.         //rb.useGravity = false; /*Sets the useGravity on the nicknamed rididdbody 'rb' to false(meaning the gameobject has no gravity)*/
    29.         Debug.Log("I'm floating!! Wheee"); /*Logs a message to the console thatsays whatevers in the paranthesis*/
    30.         jump = new Vector3(0.0f, 2.0f, 0.0f);
    31.     }
    32.  
    33.     void OnCollisionStay() {
    34.         isGrounded = true;
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update() /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "Update" and...*/
    39.     {
    40.  
    41.         if(Input.GetKeyDown(KeyCode.Space) && isGrounded) {
    42.             rb.AddForce(JumpSpeed * jump, ForceMode.Impulse);
    43.             isGrounded = false;
    44.         }
    45.  
    46.          float moveHorizontal = Input.GetAxisRaw("Horizontal");
    47.          float moveVertical = Input.GetAxisRaw("Vertical");
    48.          movement = new Vector3(moveHorizontal, 0, moveVertical);
    49.          movement = movement * InputSpeed * Time.deltaTime;
    50.     }
    51.  
    52.     public void FixedUpdate() { /*Creates a void(Which means it returns nothing) constructor nicknamed with the pre-made Unity Consstructor "FixedUpdate",
    53.     and...*/
    54.       rb.velocity = movement;//new Vector3(rb.velocity.x, rb.velocity.y, (Speed));
    55.       //rb.velocity = new Vector3((Speed), rb.velocity.y, rb.velocity.z);
    56.  
    57.         goFowards = AutomaticSpeed * Time.deltaTime;
    58.         goFowards = Mathf.Clamp(AutomaticSpeed, minSpeed, maxSpeed);
    59.         rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, (goFowards));
    60.     }
    61. }
    62.  
    I have tried putting
    Code (CSharp):
    1. rb.velocity = new Vector3
    in the if statement and [code =CSharp]movement = 0, JumpSpeed, 0[/code] in the if statement
     
  2. bosspappi

    bosspappi

    Joined:
    Jan 1, 2021
    Posts:
    8
    Is this for a FPS Controller, or 3D character?
     
  3. RealDevMashup

    RealDevMashup

    Joined:
    Oct 5, 2021
    Posts:
    8
    3d character, and I tottaly forgot to mention the player is jumping rn but he is gliding downwards instead of jumping.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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.
     
    Lurking-Ninja likes this.
  5. bosspappi

    bosspappi

    Joined:
    Jan 1, 2021
    Posts:
    8
    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?