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

How to make a ball for single jump?

Discussion in 'Scripting' started by Hridoybd, Aug 25, 2015.

  1. Hridoybd

    Hridoybd

    Joined:
    Aug 25, 2015
    Posts:
    3
    Hi,
    I have a javascript regarding controlling the ball. I want to make the ball for single jump only while press Space once or more. But it keeps jumping again and again as long as I keep pressing Space bar. I can't find any solution for making the ball for single jump until it touches the ground.
    Code (JavaScript):
    1.  
    2. var up = 30;
    3. var down = -59;
    4. var east = 30;
    5. var west = 30;
    6. var jumpHeight = 4;
    7. var rotationSpeed = 100;
    8.  
    9. function Update()
    10. {
    11. var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    12. rotation *= Time.deltaTime;
    13. rigidbody.AddRelativeTorque (Vector3.back * rotation);
    14. if (Input.GetKeyDown(KeyCode.Space))
    15. {
    16. rigidbody.velocity.y = jumpHeight;
    17. }
    18. if (Input.GetKey("up"))
    19. {
    20. rigidbody.AddForce(Vector3.forward * up);
    21. }
    22. if (Input.GetKey("down"))
    23. {
    24. rigidbody.AddForce(0,0,1*down);
    25. }
    26. if (Input.GetKey("left"))
    27. {
    28. rigidbody.AddForce(Vector3.left * east);
    29. }
    30. if (Input.GetKey("right"))
    31. {
    32. rigidbody.AddForce(Vector3.right * west);
    33. }
    34. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Create a new "grounded" boolean variable and set it to false at the top. Then in your Update, use a Raycast to test if there's an object beneath the ball. If there is, set grounded to true (or false if there's not). Finally, change your if statement to test if the jump key is pressed and grounded is true, then jump.
     
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    You could also check if you're colliding with the ground using OnCollisionStay and seeing if you're in contact with an object tagged "ground". Personally, I use a short Raycast as GroZZleR suggested, but a collision check is another option if you'd prefer.
     
    GroZZleR likes this.
  4. Hridoybd

    Hridoybd

    Joined:
    Aug 25, 2015
    Posts:
    3
    Thank you for your reply. Unfortunately, I do not know anything about Raycast. I have checked that link and understood few things but could not implement it on my given code. Would you like fix the code, I am bit puzzled :(
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    If you want a more detailed explanation of this gamesplusjames has a great set of tutorials on these exact sort of issues, it's just a matter of adapting the code to your own after watching it, if you need help with doing moving platforms he also has an episode on that.

     
  6. Hridoybd

    Hridoybd

    Joined:
    Aug 25, 2015
    Posts:
    3
    Thanks for the video, I will see what I can do from it :)
     
  7. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    For clarification, since I can see where things might be confusing if you're unfamiliar with both coding and with the docs, on the Raycast page there are multiple different ways of using it.
    You would want the second example on the page, the one that includes a RaycastHit variable, so that you can get the information about what it was that was hit.

    So something really basic would be like this:

    Code (JavaScript):
    1. var hitInfo : RaycastHit;
    2. var raycastDistance = 2.0;
    3. var onGround = false;
    4.  
    5. function Update()
    6. {
    7.     if(Physics.Raycast(transform.position, -Vector3.up, hitInfo, raycastDistance)
    8.     {
    9.         if (hitInfo.transform.tag == "ground")
    10.         {
    11.             onGround = true;
    12.         }
    13.         else
    14.         {
    15.             onGround = false;
    16.         }
    17.     }
    18. }
    The variable 'hitInfo' is where the Raycast stores the information about what it hit.
    rayCastDistance can be adjusted in the inspector to change how far down the Raycast shoots. You wouldn't want it going too far, or it'll still be hitting the ground after you jump, but too short and it'll never reach the ground at all.
    The bool 'onGround' can be checked when you want to jump. Just use an if statement that checks this first.
    There are different ways to do this, as well as better ways, but this should help to get you started.
    =)