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

Writing "Jump" from scratch

Discussion in 'Scripting' started by wh33t, Aug 30, 2013.

  1. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    Currently this is what I've got for my Jump script.

    Code (csharp):
    1.  
    2.             if(Input.GetButtonDown("Jump") )
    3.             {
    4.                 rigidbody.AddRelativeForce(transform.up * jumpForce);
    5.            
    6.                 Debug.Log ("Jumping");
    7.             }
    8.  
    The problem is that my character can jump while it's still in the air. How might I go about checking if the character is still in the air?
     
  2. Lachee1

    Lachee1

    Joined:
    Aug 21, 2012
    Posts:
    16
  3. yorgaraz

    yorgaraz

    Joined:
    Dec 27, 2012
    Posts:
    2
    Interesting, why don't you try to apply this line only if the body is touching the ground?
     
  4. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    Yes, that's the idea. Except I still want to disable jump even if the character is on a solid object that is not the "ground" such as a block or something.

    Thank you for the raycast information. That looks mostly striaght forward to. Ill try it out.
     
  5. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Btw, you're mixing local and world coordinates.
     
  6. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    Do you mean that the addrelativeforce is referencing "up" on the rigidbody instead of "up" in the game world?
     
  7. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Code (csharp):
    1.  
    2. // relative to rigidbody
    3. addRelativeForce(x * Vector3.up);  // addRelativeForce does the transformation from world -> local
    4. addForce(x * transform.up);  // transform.up does the transformation from world -> local
    5.  
    6. // relative to world
    7. addForce(x * Vector3.up);  // no transformation
    8.  
    9. // neither, since you're transforming the coordinates *twice*
    10. addRelativeForce(x * transform.up);
    11.  
    12.  
     
    Last edited: Sep 1, 2013
  8. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    I'm sorry, I don't quite understand what it means when you type "world -> local"

    I'm especially confused because what I have typed in is causing my character to jump as I expect it to.
     
    Last edited: Sep 2, 2013
  9. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Please bear in mind that using a simple Raycast to detect whether the player is grounded will cause problems when the ground isn't a flat surface. If you're walking over a small hole for example. You won't be grounded while walking over it, despite...well, walking over it. Same can apply if you're standing at the edge of something, depending on its collider.
    I've read that using CheckCapsule() instead would be the way to go, which sounds logical. The only problem is that I cannot seem to get it working. It always returns true - even if I'm in midair - despite the measurements being correct (checked with Debug.DrawLine()).
     
  10. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    World coordinates: "Up" means +y. Means in the directions of Vector3.up, ie: (0, 1, 0).

    Local coordinates: "Up" is relative to the rotation of your transform. If you are laying on your back in bed, "up" to you would be toward the wall nearest your head. "Forward" would be the direction you're facing, ie: towards the ceiling.

    A "transform" is used to transform world coordinates to local and vice versa. For the guy lying in bed, transform.forward = (0, 1, 0) = Vector3.up.

    I'm guessing your character always stands "upright". So world or local make no difference in terms of "up", they will always be the same. Sometimes code works because of outside constraints or luck, it happens.

    At any rate, you should probably google the difference between world and local coordinates before your luck runs out.
     
    Last edited: Sep 2, 2013
  11. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    Ahh, wise words. Thank you. I want to program this correctly, so the correct way to do this would be to use Vector3.up right?
     
  12. wh33t

    wh33t

    Joined:
    Feb 26, 2013
    Posts:
    44
    Update: This code below now works as expected. Ill use this until it gives me issues. Thanks for the help guys.

    Code (csharp):
    1.  
    2. if (Physics.Raycast(transform.position, -Vector3.up, 1f))
    3. {
    4.     rigidbody.AddRelativeForce(Vector3.up * jumpForce);
    5.     Debug.Log("Jumping.");
    6. }
    7. else
    8. {
    9.     Debug.Log("Can't Jump. Not Grounded.");
    10. }
    11.  
     
    Last edited: Sep 5, 2013