Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

sphere cast problem

Discussion in 'Physics' started by sahlikhalil, Jun 25, 2020.

  1. sahlikhalil

    sahlikhalil

    Joined:
    Nov 14, 2015
    Posts:
    3
    it's my first time using the sphere cast and it is not working well specially on planes
    Code (CSharp):
    1.     void OnGroundCast()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.SphereCast(transform.position, sphereCollider.radius, Vector3.down, out hit))
    5.         {
    6.             onGround = true;
    7.         }
    8.         else
    9.         {
    10.             onGround = false;
    11.         }
    12.         print(onGround);
    13.     }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    In what way is it not working well? I notice you're not specifying a max distance. Are you intending for the spherecast to only be one meter in length?

    I'd recommend adding some Debug.DrawLine to draw lines that represent your casting. It can help make sure your positions and directions are correct.
     
  3. sahlikhalil

    sahlikhalil

    Joined:
    Nov 14, 2015
    Posts:
    3
    I'm actually trying to check if it's on ground or not plus i've changed the radius to 0.001f cause i wanted to see what will happen and it worked well... so can you please explain to me why did that fix the problem cause i'm new to the collidercast and i've been using raycast the whole time
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    One possible reason I can think of is that spherecasts will not get hits if the sphere starts out already overlapping the object at the initial position of the spherecast. So, a smaller radius would reduce the chance that it's initially overlapping.

    So let's say the radius is 0.5, but you start the spherecast 0.4 meters above the ground. At that point, the sphere is already intersecting the ground, and you won't get a hit with the ground as the test moves the sphere further downward. If, instead, the radius were smaller, like 0.1, then it would not initially be overlapping the floor, and you'd likely hit the ground as long as the distance you go is large enough.

    That's just my guess. Again, it's helpful to draw things. I use Debug.DrawLine to visualize things. And if it's really causing me a headache, I'll create sphere primitives and put them in my scene (temporarily) to visualize what's going on and better understand it.
     
    homemacai likes this.
  5. sahlikhalil

    sahlikhalil

    Joined:
    Nov 14, 2015
    Posts:
    3
    Thnak you so much for the reply and i tried setting the radius to sphereCollider.radius/2 and the distance to 0.5 and it worked fine like it should so thanks for your time :)
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Sounds good. Just be careful the radius isn't larger than your character's radius, or you run the risk of hitting objects beside the player instead of below the player.
     
    sahlikhalil likes this.