Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Going Backwards and Jumping with Physics

Discussion in 'Scripting' started by jtman562, Jun 2, 2011.

  1. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    I'm having some trouble. I can move forward, left, right, and jump(sort of), but I'm having trouble going backwards. Here's my script:

    Code (csharp):
    1. var moveSpeed = 5;
    2. var jumpHight = 5;
    3.  
    4. function FixedUpdate () {
    5.   if(Input.GetAxis("forward")){  
    6.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    7.  
    8.   /*if(Input.GetAxis("backward")){  
    9.     rigidbody.AddForce (Vector3.backward * moveSpeed);}*/
    10.  
    11.   if(Input.GetAxis("left")){  
    12.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    13.    
    14.   if(Input.GetAxis("right")){  
    15.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    16.  
    17.   if(Input.GetButtonDown("Jump")){  
    18.     rigidbody.AddForce (Vector3.up * jumpHight);}
    19. }
    I have the section to go back commented out since it messes up the script. You see, the word "backward" doesn't turn blue. What do I have to put for it?

    Also, when you jump, you can keep pressing SPACE to jump as many times as you want while still in the air. How can I make it so you can only jump while you're grounded? Help would be appreciated.


    Thanks.
     
  2. OneManArmy3D

    OneManArmy3D

    Joined:
    Jun 2, 2011
    Posts:
    191
    Code (csharp):
    1. var moveSpeed = 5;
    2. var jumpHight = 5;
    3.  
    4. function FixedUpdate () {
    5.   if(Input.GetKey("w")){  
    6.     rigidbody.AddForce (Vector3.forward * moveSpeed);
    7.     }
    8.  
    9.   if(Input.GetKey("s")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);
    11.     }
    12.  
    13.   if(Input.GetKey("a")){  
    14.     rigidbody.AddForce (Vector3.left * moveSpeed);
    15.     }
    16.    
    17.   if(Input.GetKey("d")){  
    18.     rigidbody.AddForce (Vector3.right * moveSpeed);
    19.     }
    20.  
    21.   if(Input.GetKeyDown("space")){  
    22.     rigidbody.AddForce (Vector3.up * jumpHight);
    23.     }
    24. }
    About jumping:
    http://forum.unity3d.com/threads/84839-Rigidbody-jump

    P.S.(use google)
     
    Last edited: Jun 2, 2011
  3. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Ok, this is the new script:

    Code (csharp):
    1. var isFalling : boolean=true;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling){
    19.         rigidbody.velocity.y=jumpHight;
    20.     }
    21. }
    22.  
    23. function OnCollisionStay(collisionInfo : Collision) {
    24.  
    25.   if(collision.gameObject.tag == "ground"){    
    26.    
    27.     isFalling=false;}
    28.  
    I get a message that goes something like this:

    "All compiler errors must be fixed before entering play mode."

    When I look in the console, there are no errors. Any ideas?
     
  4. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Scratch that. New code:


    Code (csharp):
    1. var isFalling : boolean=true;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling == false){
    19.         rigidbody.AddForce (Vector3.up * jumpHight);
    20.     }
    21. }
    22.  
    23. function OnCollisionStay(collision : Collision) {
    24.  
    25.   if(collision.gameObject.tag == "ground"){    
    26.    
    27.     isFalling=false;}}
    28.  
    I have no more errors, but I can't jump anymore. Help?
     
  5. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    This bit of code looks suspect to me...

    Code (csharp):
    1. if(Input.GetButtonDown("Jump")  !isFalling == false){
    I'd guess that should be:

    Code (csharp):
    1. if(Input.GetButtonDown("Jump")  !isFalling){
    Also, where does isFalling get (re)set to true?
     
  6. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    It's false because I only want to be able to jump if he's not falling.
     
  7. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Ok, this is the new script:

    Code (csharp):
    1. var isFalling : boolean=false;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling == false){
    19.         rigidbody.AddForce (Vector3.up * jumpHight);
    20.     }
    21. }
    22.  
    23. function OnCollisionExit(collision : Collision){
    24. if(collision.gameObject.tag == "ground"){
    25.   isFalling == true;}}
    26.  
    27. function OnCollisionEnter(collision : Collision) {
    28.  
    29.   if(collision.gameObject.tag == "ground"){    
    30.    
    31.     isFalling == false;}}
    32.  
    33.  
    I get an error. I still don't know what's wrong.
     
  8. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    I understand what you want, but that's not what your (overly complex) statement is giving you...

    First, these two statements are equivalent:

    Code (csharp):
    1. !isFalling
    Code (csharp):
    1. isFalling == false
    Now, this (from your edited code):

    Code (csharp):
    1. !isFalling == false
    ..basically says:

    1. Take the current value of "isFalling" and negate it (so, true becomes false and false becomes true).
    2. Take the negated value and see if it's equal to "false".

    So, in order for the above statement to resolve to "true", isFalling must already be true. So, the above is actually the same is just testing "isFalling" by itself. So, the full statement is saying "If the jump button is pressed and I'm currently falling, then jump". As you can see, its 1) not what you want, and 2) overly complex.

    I think my original suggestion is more what you're after, though my original question still stands. Where does isFalling get reset to "true". It should probably happen in the above-discussed code after you've triggered the jump code.

    Jeff
     
  9. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Ok. Tomorrow I'll test it and let you know what happens.
     
  10. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Ok, I fixed that, but I still get the error. Here's the whole code:

    Code (csharp):
    1. var isFalling : boolean=false;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling){
    19.         rigidbody.AddForce (Vector3.up * jumpHight);
    20.     }
    21. }
    22.  
    23. function OnCollisionExit(collision : Collision){
    24. if(collision.gameObject.tag == "ground"){
    25.   isFalling == true;}}
    26.  
    27. function OnCollisionEnter(collision : Collision) {
    28.  
    29.   if(collision.gameObject.tag == "ground"){    
    30.    
    31.     isFalling == false;}}
    32.  
    33.  
    Ideas?
     
  11. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    There isn't a Vector3.left, you need to use -Vector3.right instead :)

    Oh, and the double equals sign (i.e. == ) is used to compare whether something equals something else, which is why it's used in if statements. Use one equals sign (just = ) when you want to set a variable to be a value.

    So your isFalling definitions should be...

    isFalling = false;

    isFalling = false;
     
    Last edited: Jun 3, 2011
  12. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Well, Vector3.left works fine. I only started getting errors when I tried to add jumping.
     
  13. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Ok, no more errors, but I can't jump. Here's the code:

    Code (csharp):
    1. var isFalling : boolean=false;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling){
    19.         rigidbody.AddForce (Vector3.up * jumpHight);
    20.     }
    21. }
    22.  
    23. function OnCollisionExit(collision : Collision){
    24. if(collision.gameObject.tag == "ground"){
    25.   isFalling = true;}}
    26.  
    27. function OnCollisionEnter(collision : Collision) {
    28.  
    29.   if(collision.gameObject.tag == "ground"){    
    30.    
    31.     isFalling = false;}}
     
  14. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    If you continue to have trouble, please be specific about the error(s) you're getting. Don't make us guess... :)
     
  15. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Like I said, I don't have any errors. I just can't jump. I think I have my trues and falses in the wrong places. I just want to be able to jump if I'm touching the ground. I really need some help.
     
  16. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Firstly, add some debug code to your jumping bit, to check if it's getting called at all.

    Debug.Log("Jump pressed!");

    Then, if that gets printed in the the console when you hit jump, try increasing the jump force considerably. Could be that the force you're adding just isn't strong enough to overcome gravity.
     
  17. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    This part of the script:

    Works fine, but I still can't jump and the rest of the script that controls jumping isn't being reached. Help?

    Here is the whole script:
    Code (csharp):
    1. var isFalling : boolean=false;
    2. var moveSpeed = 5;
    3. var jumpHight = 5;
    4.  
    5. function FixedUpdate () {
    6.   if(Input.GetAxis("forward")){  
    7.     rigidbody.AddForce (Vector3.forward * moveSpeed);}
    8.  
    9.   if(Input.GetAxis("backward")){  
    10.     rigidbody.AddForce (-Vector3.forward * moveSpeed);}
    11.  
    12.   if(Input.GetAxis("left")){  
    13.     rigidbody.AddForce (Vector3.left * moveSpeed);}
    14.    
    15.   if(Input.GetAxis("right")){  
    16.     rigidbody.AddForce (Vector3.right * moveSpeed);}
    17.  
    18.   if(Input.GetButtonDown("Jump")  !isFalling){
    19.         rigidbody.AddForce (Vector3.up * jumpHight);
    20.     Debug.Log("Jump Pressed");
    21.     }
    22. }
    23.  
    24.  
    25. function OnCollisionEnter(collision : Collision) {
    26.  
    27.   if(collision.gameObject.tag == "ground"){    
    28.    
    29.     isFalling = false;
    30.     Debug.Log("You're on the Ground");}}
    31.  
    32. function OnCollisionExit(collision : Collision){
    33. if(collision.gameObject.tag == "ground"){
    34.   isFalling = true;
    35.   Debug.Log("You Left the Ground");}}
    36.  
     
  18. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Try setting jumpHight to something like 100 - see if it does anything.
     
  19. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Nothin.
     
  20. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    Scratch that. I figured it out. Thanks for the help.