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. Dismiss Notice

Adding jumping to the roll-a-ball tutorial.

Discussion in 'Scripting' started by Spiv5, Aug 7, 2016.

  1. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    Hello, I would like to add jumping to the roll a ball tutorial. I have decided to expand on the game, and jumping is what I want to do next.

    Code (CSharp):
    1. void FixedUpdate ()
    2.     {
    3.         float moveHorizontal = Input.GetAxis ("Horizontal");
    4.         float moveVertical = Input.GetAxis ("Vertical");
    5.  
    6.         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
    7.  
    8.         rb.AddForce (movement * speed);
    9.    
    10.  
    How would I add jumping to this?
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Add an if statement to check for whatever key you want to use as the jump & add an impulse force vector3.up*[whatever jump force you want]

    You will probably need a large jump force to get it to show, maybe start with a force of 5000 to see how it goes. You will also want to include a check that the ball is grounded in your if statement otherwise it will jump every time you press the jump key even if the ball is in midair.
     
  3. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    Code (CSharp):
    1.     if (other.gameObject.CompareTag("Ground"))
    2.     {
    3.       grounded = true;
    4.     }
    5.     else
    6.     {
    7.       grounded = false;
    8.     }
    I have done this to check for grounded yet it does not work. I am working on the jump now​
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Where are you doing that check? In OnCollisionStay ?
     
  5. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    I was actually doing it on OnTriggerEnter, similar to the pickups. I'll try that now, thanks for putting up with me
     
  6. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    Code (CSharp):
    1.       void OnCollisionStay(Collision other)
    2.       {
    3.           if (other.gameObject.CompareTag("Ground"))
    4.           {
    5.               grounded = true;
    6.           }
    7.       }
    8.  
    9.       void OnCollisionExit(Collision other)
    10.       {
    11.           if(other.gameObject.CompareTag("Ground"))
    12.           {
    13.               grounded = false;
    14.           }
    15.       }
    This isn't working, it says its always grounded
     
  7. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    Its the collision exit code, it doesn't run it (Checked with print in console)
     
  8. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Thats because you are checking for a floor collision when you are no longer touching the floor!
    Try just removing the if check and just setting grounded to false on collision exit.
     
  9. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    I ended up just doing it with Raycasting, I'll post the code I use
    Code (CSharp):
    1.  
    2.         if (Input.GetKeyDown("space"))
    3.         {
    4.             RaycastHit hit;
    5.             if(Physics.Raycast(transform.position,-Vector3.up, out hit, 0.5f))
    6.             {
    7.                 grounded = true;
    8.             }
    9.             else
    10.             {
    11.                 Debug.Log("Nothing Below");
    12.                 grounded = false;
    13.             }
    14.  
    15.             if (grounded == true)
    16.             {
    17.                 rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    18.                 jump.Play();
     
    tedthebug likes this.