Search Unity

Question Regarding Ground Detection (solved)

Discussion in 'General Discussion' started by rd1349, Apr 29, 2017.

  1. rd1349

    rd1349

    Joined:
    Apr 25, 2017
    Posts:
    30
    So I'm new to unity but I have some programming experience. I know I need to make a couple modular scripts for general use. This one is the first I've run into a snag over. For the life of me I just can't see what I'm getting wrong. It's a work in progress and I'm testing on a sphere and I'm not quite understanding why the CheckSphere is not colliding with the plane underneath it.

    //coll is the collider for the gameobject

    //Check Grounded Function
    private bool CheckGrounded()
    {
    return Physics.CheckSphere(new Vector3 (transform.position.x, transform.position.y-0.1f, transform.position.z),coll.bounds.extents.x,8);
    }

    Thanks to anyone who replies, I've been stuck on this for about an hour and a half.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,572
    A better way to detect ground is to shoot down ray and check distance to collision.

    In your potential issues are ion :coll.bounds.extents.x (what does it return? have you checked it?) or hard-coding layer mask to 8.

    Set layer mask to -1 and see what happens. "8" most likely refers to second layer, which might be "IgnoreRaycast".
     
  3. rd1349

    rd1349

    Joined:
    Apr 25, 2017
    Posts:
    30
    Thanks again for the reply man. I've already set 8 to ignore the gameobject so it's not that. As for coll.bounds.extents.x it's I've noticed some strange things with that so I'll continue testing per the advice. It's supposed to define the size of the sphere but it seems that no matter how large I make it it still doesn't collide.
    I'm trying to use a sphere instead of raycast because of the possibilities of uneven terrain and small holes. It's more for contingency sake than anything else.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,572
    I would replace that with a fixed number and see if it works. Basically... your code does not show how you initialzie "coll". Unity (IIRC) does not have such built-in variable, so it must be a member variable that is initialized elsewhere.

    There's SphereCast. It is like raycast, but with radius. The one gotcha with it is that you need to move ray start up if a bit. Meaning if object's "floor coordinates" are at X, Y, Z and you want to shoot spherecast down to check if there's a floor, you'd use (X, Y + sphereRadius + someSmallValue, Z) for starting coordinates.

    https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
     
  5. rd1349

    rd1349

    Joined:
    Apr 25, 2017
    Posts:
    30
    Well got it done with SphereCast. Thanks for the tip. I had to fiddle around a lot with everything a lot but I got it working. Whew I got a lot to learn still. Anyway I'm going to get back to it.Thanks again.