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

Jump using Rigidbody not working (C#)

Discussion in 'Scripting' started by Tacosaurus, May 13, 2015.

  1. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    I don't really see a problem with the code, and there aren't any errors in the console. But whenever I try to jump, nothing happens. Any suggestions or help would be awesome! :)
    Code (CSharp):
    1. private CharacterController controller;
    2.     public float speed = 6.0f;
    3.     private Vector3 moveDir = Vector3.zero;
    4.     public float jumpSpd = 200.0f;
    5.     public Rigidbody rb;
    6.     private float gravity = 9.81f;
    7.  
    8.     void Start(){
    9.         controller = GetComponent<CharacterController>();
    10.         rb.GetComponent<Rigidbody>();
    11.     }
    12.    
    13.     void Update () {
    14.         Move ();
    15.     }
    16.  
    17.     void Move(){
    18.         //makes the Vector3 use the WASD inputs
    19.         moveDir = new Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
    20.         //creates a transform out of the input
    21.         moveDir = transform.TransformDirection(moveDir);
    22.         //controls how high or low the transform #'s are
    23.         moveDir *= speed;
    24.         //push down at a constant rate
    25.         moveDir.y -= gravity;
    26.         //If I jump the gravity will still be in effect
    27.         if (Input.GetKey (KeyCode.Space)){
    28.             //push up for 10 "steps" at the jump speed
    29.             rb.AddForce(Vector3.up * jumpSpd, ForceMode.Impulse);
    30.         }
    31.         //move the controller
    32.         controller.Move(moveDir * Time.deltaTime);
    33.     }
     
  2. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    Since you are already using Move function you could modify Y axis of the MoveDir to make your character jump!
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.Space)) {
    2. moveDir.y = jumpSpd;
    3. }
     
  3. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Well, I'm not sure about why but I'm wondering why do you do gravity manually ?

    You're moving your object down -9.81f every update, without even using Time.deltaTime.

    Code (CSharp):
    1.  
    2.         //push down at a constant rate
    3.         moveDir.y -= gravity;
    If your GameObject has a RigidBody attached to it, you should be able to enable "Use Gravity". That will do the work for you and it will do it correctly.

     
  4. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    So, if "Use Gravity" is enabled, then I don't have to worry about it in code? awesome.
    Also,
    I am now going to work towards using only the rigidbody and physics for movement. I'll post the updated code when I can if anyone wants to continue monitoring the thread.
    Thanks Guys!