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

Weird Jump Bug

Discussion in 'Scripting' started by Brandon-Keeler, Oct 27, 2014.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    So I am trying to make my game object jump. If I wait for a few seconds after my game starts, my code works just as intended. However, if i try to jump immediately after the game starts, I end up flying indefinitely, without falling.
    This is my code...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6. public GameObject player;
    7. public float speed;
    8. public float value = 0;
    9. bool isGrounded = false;
    10.  
    11.  
    12.      void OnCollisionEnter (Collision col)
    13.      {
    14.           if(col.gameObject.name == "Ground 1")
    15.           {
    16.           isGrounded = true;
    17.           }
    18.      }
    19.  
    20.      void OnCollisionExit (Collision col)
    21.      {
    22.           if(col.gameObject.name == "Ground 1")
    23.           {
    24.           isGrounded = false;
    25.           }
    26.      }
    27.  
    28.      void FixedUpdate ()
    29.      {
    30.      float moveHorizontal = Input.GetAxis ("Horizontal");
    31.      float moveVertical = Input.GetAxis ("Vertical");
    32.  
    33.           if(isGrounded == true)
    34.           {
    35.                if (Input.GetKeyDown (KeyCode.Space))
    36.                {value = 9.8f * 3.0f;}
    37.                else {value = 0;}
    38.           }
    39.  
    40.      Vector3 movement = new Vector3 (moveHorizontal, value , moveVertical);
    41.      rigidbody.AddForce(movement * speed * Time.deltaTime);
    42.      }
    43. }
     
    Last edited: Oct 28, 2014
  2. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    Can someone tell me what is going on and how I can fix my code?
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    I am guessing it has something to do with a FixedUpdate(). I undestand that adding forces works best with FixedUpdate(), but try experimenting with that.
     
  4. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    I recreated your project, and I go flying up without hitting anything... pretty funny actually.

    1) If value is set to a positive number in the inspector, all is lost... you aren't making sure value is zero when it starts.
    2) You only set value = 0 if grounded = true, which if the player begins moving upward at the start of the scene, grounded will never be true and therefore value will never be a nonpositive number.

    Do you understand?
     
  5. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I'm not sure I'm getting you... in line 8 i set value = 0; What can i change in my code to keep it from flying up.
     
  6. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    You are only setting value to 0 if the player is on the ground. You're setting isGrounded = false; only when an OnCollisionExit() is executed, have you ever considered that that method may not get called like it's supposed to?

    Change your code to check for collision with the ground, and if there's no collision set grounded to false. Add some debug.log calls so you can monitor the state of your variables in real time, notice any weird changes (a number is not zero when you're on the ground, for example).

    I'm not going to write your script for you, though. That's no good for anyone.
     
    Deleted User likes this.
  7. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    Ok so I did this, and it no longer jumps forever, however if i try to jump immidiately after the game starts i jump twice as high


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. //Bug with player jumping infinately if player attemps to jump without hitting the ground first.
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     //Update is called once per frame
    7.     public GameObject player;
    8.     public float speed;
    9.     public float value = 0;
    10.     bool isGrounded = false;
    11.  
    12.  
    13.     void OnCollisionEnter (Collision col)
    14.     {
    15.         if(col.gameObject.name == "Ground 1")
    16.         {
    17.             isGrounded = true;
    18.             //print ("Collision");
    19.             //Destroy(col.gameObject);
    20.         }
    21.     }
    22.    
    23.     void OnCollisionExit (Collision col)
    24.     {
    25.         if(col.gameObject.name == "Ground 1")
    26.         {
    27.             isGrounded = false;
    28.         }
    29.     }
    30.    
    31.     void FixedUpdate ()
    32.     {
    33.         float moveHorizontal = Input.GetAxis ("Horizontal");
    34.         float moveVertical = Input.GetAxis ("Vertical");
    35.  
    36.         if(isGrounded == true)
    37.         {
    38.             if (Input.GetKeyDown (KeyCode.Space))
    39.             {value = 9.8f * 3.0f;}
    40.             else {value = 0;}
    41.         }
    42.         if(isGrounded == false)
    43.         {
    44.             value = 0;
    45.         }
    46.  
    47.         Vector3 movement = new Vector3 (moveHorizontal, value , moveVertical);
    48.         rigidbody.AddForce(movement * speed * Time.deltaTime);
    49.     }
    50. }
     
  8. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    I think it's cycling through fixed update 2 times before the collision exit event is raised even though you are now only setting the value once you are actually adding force to the rigid body more than once this is because it still takes time for your body to move off of the platform and raised the on collision exit event.

    Just think about it you'll get it
     
  9. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I tested it, and it thinks it is making an impact in the ground while in mid air at the start of the program. This only happens at the start if I try to jump Immediately after I start playing.
     
  10. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    I dunno, lower the time scale and monitor your variables, something funky is happening and it's definitely something that you can fix.