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. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question boxcast not working?

Discussion in 'Scripting' started by AerionXI, Sep 23, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    335
    i'm trying to add collision to my character in 3D, and for some reason my boxcast is not returning true even though I have the ground layer set to "ground", and whatisGround is set to the proper ground layer mask.

    Here's my script

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour {
    2.  
    3.     public Collider m_Collider;
    4.     public RaycastHit m_Hit;
    5.  
    6.     public void Awake ( ) {
    7.         m_Collider = GetComponent <Collider> ( );
    8.     }
    9.  
    10.     public bool IsGrounded ( ) {
    11.  
    12.         // Fetch the center of the Collider volume
    13.         m_Center = m_Collider.bounds.center;
    14.  
    15.         // Fetch the size of the Collider volume
    16.         m_Size = m_Collider.bounds.size;
    17.  
    18.         m_SizeY = ( ( m_Size.y / 2.0f ) + 0.01f );
    19.  
    20.         m_Min = m_Collider.bounds.min;
    21.         m_Max = m_Collider.bounds.max;
    22.  
    23.         var colliderExtentsY = m_Collider.bounds.extents.y;
    24.         var rayPosition = ( m_Center );
    25.         var rayDirection = ( -transform.up * m_SizeY );
    26.  
    27.         isGrounded = Physics.BoxCast (
    28.             rayPosition, m_Size,
    29.             rayDirection, out m_Hit,
    30.             transform.rotation, m_MaxDistance,
    31.             whatIsGround
    32.         );
    33.  
    34.         return isGrounded;
    35.  
    36.     }
    37.  
    38.     public void Update ( ) {
    39.  
    40.         Debug.Log (
    41.             $"IsGrounded ( ) : { IsGrounded ( ) }"
    42.         );
    43.  
    44.     }
    45.  
    46. }
    Any help is absolutely appreciated!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,320
    You probably want to use OverlapBox, not BoxCast. If BoxCast (or any of the casts) are already overlapping a collider it won't count as 'hitting' it.
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    335
    It's not overlapping any collider. it's overlapping a layer mask ( ground ).
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,881
    That doesn't make sense as a reply TBH. You don't overlap a layer-mask. A layer-mask is an argument specifying what you want to detect you overlap with.

    The above suggestion was to use the OverlapBox test which gives you a yes/no answer rather than the cast which as also mentioned, won't detect things it starts overlapped with.
     
    PraetorBlue likes this.
  5. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    335
    I don't see any problem with using Boxcast. I will try Overlap, but right now I just want to know how to properly use Boxcast in my example.
     
  6. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    335
    I'm teaching myself casting collision methods though. So I would like to know how to fix what I already have.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,320
    You've been told plenty. If your box cast starts already overlapping with the ground, it won't detect a hit with the ground.

    It goes without saying that the ground needs to have a collider on it too.
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,881
    Well, this was said...
    Help is of no use if the replies are not read. :( If a particular point isn't understood then always feel free to ask about the reply itself or state you don't follow. This stuff can be confusing for sure but there's gold in the replies with little digging. :)
     
    Kurt-Dekker likes this.
unityunity