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

Question I don't understand how SphereCast works, even after reading doc about it

Discussion in 'Scripting' started by Benji23245, Jul 14, 2023.

  1. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    127
    Hello everyone !
    In my project I've been using SphereCasts but don't really understand how they function.

    But right now I'm facing an issue where a SphereCast really would be the easy solution but I can't get it to work.

    Basically I have a rail (a trigger), with a LayerMask "Grind", and so far I've been using a Raycast to make it so that if it returns true, you should be teleported to the grindHit.point and get into a "Grind" state (I'm sparing you all the other details).

    It works like a charm.

    But I realized that I could only enter rails that were directly below my player that way. Whereas I want to enter rails if they are vertical and my player is falling and touching them. And so I figured this would work :

    Code (CSharp):
    1. Physics.SphereCast(transform.position, physics.playerCollider.radius * 1.25f, -transform.up, out grindHit, maxLength, grindMask, QueryTriggerInteraction.Collide);
    But it doesn't, it doesn't register any hit, no matter how I land on any rail. Although that Raycast worked perfectly :

    Code (CSharp):
    1. Physics.Raycast(transform.position, -transform.up, out grindHit, maxLength, grindMask, QueryTriggerInteraction.Collide);
    What is it that I don't understand in the SphereCast behavior, and how could I make it work the way I want to without having to cast 20 Raycasts ?

    Or maybe I should use something different like OverlapBox/Sphere/Caspule ?

    Thank you !
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You can think of any 'cast' method as sending an imaginary shape along a direction, and checking it 'hits' anything along that path. Note that if it's already intersecting with something when the cast starts, that won't count as a hit.

    Most likely, yes. If you want to check if something is within a certain space, use the overlap methods.
     
    Bunny83 likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    Possibly because it's start position should be higher. At least the radius size.
     
    Bunny83 likes this.
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Spherecast is like, you're trying to hit the number pad of your cell phone but sometimes you're hitting between the numbers and get no trigger cos you're using just a needle sharp pointer. You need to bulkify your probe to a finger that's round in the fingertip, so you'll hit something even when it's not precise.
     
  5. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    127
    Thank you all for all your answers !

    It appears the OverlapCapsule did the trick ! :)