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 Is there any way to visualise a CapsuleCast?

Discussion in 'Scripting' started by TheProcessYet_64, Oct 2, 2023.

  1. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    I'm having trouble getting a CapsuleCast to work. I am trying to position it horizontally around the bottom of my player but since I can't visualise it I don't know what's going wrong.

    How it's supposed to look is:
    upload_2023-10-3_9-37-44.png

    Code (CSharp):
    1. Vector3 p1 = transform.position + transform.up * -0.8f; // position near bottom of player
    2. Vector3 p2 = Vector3.up * normalCenter;
    3. if (Physics.CapsuleCast(p1, p2, 0.5f, Vector3.down, out hit, 5f))
    4. {
    5.     Debug.Log("Cast";
    6. }
    But if I understand correctly, p1 and p2 are supposed to be two points so I can partially see why it doesn't work, but I also get an error on line 1 saying "can't apply * to Vector3 and Vector3 types", even though this same code works for a SphereCast.
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    If you want to aim straight down (common for detecting how far down the ground is) then a sphere-cast is simpler. The trick is to realize that the bottom of your capsule is a 1/2-sphere. To find the spot use (I think): the center of your capsule, then subtract 1/2 the height from y (putting you at the very bottom), then add back the radius (putting you exactly at the top of the bottom 1/2-sphere). That way the sphere lines up exactly with the bottom of the capsule (which matters if you're measuring distance until it hits something). Of course, the radius is the radius of your capsule.

    The reason a sphere is better for going down is that both things make an identical perfectly round tunnel, and capsule-casts are more inputs.

    A forward capsule-cast makes a tall capsule-shaped tunnel, perfect for "can my forwards-moving capsule fit". p1 is bottom center of the top 1/2-sphere (same math as for the capsule, but center.y+height/2-radius) and p2 is the top of the bottom 1/2-sphere (exact same math as the sphere-cast). It's not that much worse than a capsule cast, but why bother unless you need to?

    Of course, p1 and p2 don't have to be straight up/down from each other. Putting them at an angle puts the imaginary capsule at that angle (I think), but there's almost never any reason to do that.
     
    Bunny83 likes this.
  3. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    42
    Thanks for the detailed response. I'm going to try and use what you said to readjust my two Vectors.

    The reason for choosing capsule over sphere is because of how terrible the default character controller collision is so I'm just trying to find my own workaround so that the controller doesn't get stuck on every single object. Thanks again