- Home /
Grounded Jumping scripting problem.
I'm including the part of the code which handles the jumping. I was messing with my code to find the best method of jumping and I noticed that if I hit the jumpkey fast enough I could continue to jump and basically hover around. Then it got worse as I tried to fix it (though I think I just altered gravity) and now he jumps anytime. Basically the grounded function isn't working. I copied it off a tutorial so I'm not sure why.
     public class PlayerControl : MonoBehaviour
     {
         [HideInInspector]
         public bool jump = false;                // Condition for whether the player should jump.
         
         public float moveForce = 100;            // Amount of force added to move the player left and right.
         public float maxSpeed = 10;                // The fastest the player can travel in the x axis.
         public float jumpForce = 700;            // Amount of force added when the player jumps.
         public Transform graphics;
         public float pushPower = 2;
 
         public SkeletonAnimation skeletonAnimation;
         private Transform groundCheck;            // A position marking where to check if the player is grounded.
         private bool grounded = false;            // Whether or not the player is grounded.
         
 
 
         Quaternion goalRotation = Quaternion.identity;
         
 
         string currentAnimation = "";
 
         
         void Awake()
         {
             // Setting up references.
             groundCheck = transform.Find("groundCheck");
             
 
         }
         
         
         void Update()
         {
             // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
         grounded = Physics2D.Raycast(transform.position, groundCheck.position, 1 << LayerMask.GetMask("Ground"));  
             
                             
             // If the jump button is pressed and the player is grounded then the player should jump.
             if (Input.GetButtonDown ("Jump") && grounded) {
             jump = true;
         }
                 
                 
                 
         }
Answer by robertbu · Oct 10, 2014 at 12:50 PM
Raycast2D.Raycast() returns a RaycastHit2D which is a struct. A struct will never be null. You need to check the collider. Something like:
  grounded = (Physics2D.Raycast(transform.position, groundCheck.position, 1 << LayerMask.GetMask("Ground")).collider != null); 
Note if ground is a single object, you can use Collider.Raycast() instead of layers and Physics.Raycast().
NullReferenceException: Object reference not set to an instance of an object PlayerControl.Update () this problem is occurring
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                