Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to cast sphere cast without "out".

Discussion in 'Physics' started by wannSleep, Feb 27, 2024.

  1. wannSleep

    wannSleep

    Joined:
    Jan 18, 2024
    Posts:
    6
    I need to check, if player hits ceiling. So there is my spherecast call:
    if (!Physics.SphereCast(capsuleColider.transform.TransformPoint(capsuleColider.center), sphereCastRadius, Vector3.up, capsuleColider.height * 0.5f + 0, 1, whatIsGround))


    But i have a compile error "argument 4 must contain "out"". I tried using Ray ray instead of first two arguments, but it didnt work neither
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
  3. wannSleep

    wannSleep

    Joined:
    Jan 18, 2024
    Posts:
    6
    Yes, but when i use another function overload:
    public static bool (SphereCast ray, float radius, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

    i have the same problem and i cant figure out why
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    Just show code please, I cannot read what the above is.

    Here's how to post code. Honestly though, if you're new to C# and how to call method then you should look at some tutorials TBH. If you provide the correct argument types then it'll work. Compiler errors mean you're just using it wrong.
     
  5. wannSleep

    wannSleep

    Joined:
    Jan 18, 2024
    Posts:
    6
    Ok, so i'l explain it more clear. I'm using the codeline:
    Code (CSharp):
    1. if (!Physics.SphereCast(capsuleColider.transform.TransformPoint(capsuleColider.center), sphereCastRadius, Vector3.up, capsuleColider.height * 0.5f + 0.1, whatIsGround))
    so i know that this finction doesnt have such overload, but the unity docs says that i can use this:
    Code (CSharp):
    1. SphereCast(Ray ray, float radius, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
    but when i rewrite my spherecast code like that:
    Code (CSharp):
    1. Ray ray = new(capsuleColider.transform.TransformPoint(capsuleColider.center), Vector3.up);
    2. if (!Physics.SphereCast(ray, sphereCastRadius, capsuleColider.height * 0.5f + 0.1, whatIsGround))
    but I still have the same compile error as before. I dont understand why.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    Ensure the types are correct.

    ray = Ray
    sphereCastRadius = float
    capsuleColider.height * 0.5f + 0.1 = float (note that 0.1 is double, not float)
    whatIsGround) = int (LayerMask)

    If the types are correct then the compiler will know which you mean. If they're not, it has no idea so is just guessing and what you might mean from three overloads.