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

Script Help

Discussion in 'Scripting' started by Abel_Graham, May 28, 2014.

  1. Abel_Graham

    Abel_Graham

    Joined:
    May 3, 2014
    Posts:
    10
    I need help on a 2d player controller script in C#, for some reason the player can jump as many times as they want up to the point where they are 1 tick (I don't know what to call the values :?) above the block in the Y axis, any help? Here is the script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 1;
    8.     public float jumpSpeed = 10;
    9.    
    10.     private bool isFalling = false;
    11.     private float RayCastModifier = 0.01f;
    12.    
    13.     void Update()
    14.     {
    15.         if (Input.GetKey(KeyCode.D))
    16.         {
    17.             transform.position += (transform.right * moveSpeed) * Time.deltaTime;   //We're multiplying with deltaTime so that higher framerate != higher speed.
    18.         }
    19.         else if (Input.GetKey(KeyCode.A))
    20.         {
    21.             transform.position -= (transform.right * moveSpeed) * Time.deltaTime;
    22.         }
    23.         RaycastHit2D hit = Physics2D.Raycast (transform.position, -Vector2.up, transform.lossyScale.y + RayCastModifier, 1 << LayerMask.NameToLayer("Environment"));
    24.         Debug.Log(hit.collider);
    25.         if (hit.collider != null)
    26.         {
    27.             isFalling = false;
    28.         }
    29.         else
    30.         {
    31.             isFalling = true;
    32.         }
    33.        
    34.         if (Input.GetKeyDown(KeyCode.Space)  isFalling == false)
    35.         {
    36.             if (GetComponent<Rigidbody2D>())
    37.             {
    38.                 rigidbody2D.AddForce(Vector2.up * jumpSpeed);
    39.             }
    40.             else
    41.             {
    42.                 Debug.LogError("You're trying to access a Rigidbody2D component but " + gameObject.name + " doesn't have it!");
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    48.  
    49.  
     
    Last edited: May 28, 2014
  2. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
    You need to check if hit is null.

    If the raycast doesn't hit anything, it returns null. Any attempt to access a null value will fail, throwing an exception and stopping the rest of your Update code from running.

    You need to replace this code:

    Code (csharp):
    1.         Debug.Log(hit.collider);
    2.         if (hit.collider != null)
    3.         {
    4.             isFalling = false;
    5.         }
    6.         else
    7.         {
    8.             isFalling = true;
    9.         }
    With something more like this:

    Code (csharp):
    1.         if (hit == null)
    2.         {
    3.             isFalling = true;
    4.         }
    5.         else
    6.         {
    7.             Debug.Log(hit.collider);
    8.             isFalling = false;
    9.         }
     
  3. Abel_Graham

    Abel_Graham

    Joined:
    May 3, 2014
    Posts:
    10
    After doing that, it made the player possible to jump as many times as they want no matter want...
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    RaycastHit2D is a struct so it won't be null. If you read the documentation you'll notice that the collider field will be null if nothing is hit.

    The player can jump when they are 1 unit above the ground because that's the length of your ray (-Vector3.up). Raycast a smaller distance - like to the bottom of the character - for better results.