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

Question Jump

Discussion in 'Scripting' started by Fuocartex, Jun 3, 2020.

  1. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    with this script, if I hold down space the player continues to go up to infinity what can I put so that it goes up to a set height
    thanks
    Code (CSharp):
    1.    void Jump()
    2.     {
    3.  
    4.         if (jumpInput>0 && Grounded())
    5.          {
    6.              velocity.y = moveSetting.jumpVel;
    7.           }
    8.          else if (jumpInput==0)
    9.          {
    10.              //zero out our velocity.y
    11.              velocity.y = 0;
    12.              if (transform.position.y > 0)
    13.              {
    14.                  Debug.Log("3funziona");
    15.                  //decrease velocity.y
    16.                  velocity.y -= physSetting.downAccel;
    17.              }
    18.       }
    19.  
     
  2. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    Could you post the code for the Grounded() function?
     
  3. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    Code (CSharp):
    1.  bool Grounded()
    2.     {
    3.         return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
    4.     }
     
  4. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    I can't draw any real conclusions from what you have there. I would assume that your Grounded function is always returning true. I would start checking the length of distToGrounded and the layers of the other objects your raycast could be interacting with.
     
  5. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    I just checked and if transform.position.y> 6 and I repeat the bottom space y position doesn't increase, but if I hold it down before it will
    i don't know why
    the scene is empty there is only a cube
     
  6. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    What is calling your Jump() function?
     
  7. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    update
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Might wanna start schprinkling in Debug.Log() calls to check:

    - is a particular piece of code even running?
    - what is the value of specific booleans, such as the result of Grounded()

    Also, know that
    void update()
    will not be called. It has to be
    void Update()


    Also, keep in mind Grounded() might be hitting the player or some part of the player and thinking "yup, I'm grounded. You can turn off the player's collider during the Grounded() call, or else use layermasks to limit what Raycast can hit.

    ALSO: Physics.Raycast() has about 57,000 overloads that mix and match parameters... you should used named arguments for the optional ones to make sure you're not passing flags as a distance or distance as flags, for instance.