1

I am trying to detect if a user is grounded. I have a GameObject on the screen with a BoxCollider2D component on it and is on the Ground Layer. I then have a character with a Rigidbody2D and CircleCollider2D on it along with this class:

public class CharacterEnvironment : MonoBehaviour {

    float distToGround;

    // Use this for initialization
    void Start() {
        var collider = GetComponent<CircleCollider2D>();
        distToGround = collider.bounds.extents.y;
    }

    // Update is called once per frame
    void Update() {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, distToGround + 0.1f, LayerMask.NameToLayer("Ground"));
        Debug.Log(hit.collider);
        if (hit && hit.collider != null) {
            Debug.Log(hit.collider);
        }
    }
}

When my raycast runs, hit.collider is always null, even when the object is resting on top of the Ground Object. Am I doing the calculations wrong?

Edit: I have added the check for hit in the if statement.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • `Physics2D.Raycast` returns a bool, indicating whether it has hit anything or not. The first thing you should do is check that return value before proceeding, or you'll be checking for a collider all the time regardless of whether anything was hit. – Serlite Mar 31 '17 at 21:19
  • It doesn't return a bool, `Physics.Raycast` returns a bool. It returns a `RaycastHit2D` – Get Off My Lawn Mar 31 '17 at 21:32
  • The second duplicated answer shows that you have to check if `hit` is `null` before checking it's collider or other components of the possible object hit. That's why you getting that error. If you fix it and cannot detect the floor, ask a new question with the new fixed code. – Programmer Mar 31 '17 at 21:32
  • @Serlite Not true. `Physics2D.Raycast` returns `RaycastHit2D` while `Physics.Raycast` returns a boolean value. – Programmer Mar 31 '17 at 21:33
  • @Programmer Even when I also check for `hit`, and the player is sitting on a ground piece, I still get null, and it never goes into my if statement. – Get Off My Lawn Mar 31 '17 at 21:38
  • Oh sorry, didn't read closely enough - admittedly I don't use Physics2D classes very often. Good to know! – Serlite Mar 31 '17 at 21:41
  • @GetOffMyLawn You should have put **EDIT** in your question then add the modified code. Otherwise, your current edit will make all my comments and the duplicated answer to be useless. The current problem in your code is `Debug.Log(hit.collider);` that is outside the if statement. That must also be in the if statement that checks if `hit` is `null`. Please take your time and read the second duplicated answer. – Programmer Mar 31 '17 at 21:45
  • @Programmer Neither of those helped with my issue... I am still not able to enter the `if` statement when my character lands on top of a `Ground` object. – Get Off My Lawn Mar 31 '17 at 21:55
  • Your original problem is `null` exception. The duplicated answer should solve that. Like I said on my other comment, I you have other problems create a new question with your fixed code. – Programmer Mar 31 '17 at 22:00
  • I had no issues with the `hit` being `null`, that was/is not the issue to the problem at all, and opening a new issues would be me just opening the exact same question. – Get Off My Lawn Mar 31 '17 at 22:07
  • The title and body of your questions describes a null problem when checking object hit. I can't force you to ask a new question but there are other ways to detect when player is touching the floor. See [here](http://stackoverflow.com/a/39898003/3785314) and [here](http://stackoverflow.com/a/42911357/3785314). Happy coding! – Programmer Apr 01 '17 at 01:37
  • I was writing an answer for other question before it got deleted. Did you find a solution? – Programmer Apr 02 '17 at 20:02
  • @Programmer Yeah, the issue was because `LayerMask.NameToLayer` returns the index of the layer. I needed to pass a `LayerMask` to the last parameter. So I added `public LayerMask groundLayer` and passed that value to the method. This fixed the issue. – Get Off My Lawn Apr 02 '17 at 21:03
  • Ok. Good. I though that was the problem too. – Programmer Apr 03 '17 at 02:11

0 Answers0