Search Unity

Whats CapsuleCast and SphereCast intended detection behavior? (video inside)

Discussion in 'Physics' started by HiddenMonk, Feb 27, 2015.

  1. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    EDIT - The following is in Unity 4.6, I think this has been fixed in Unity 5

    I was working on a CapsuleCastAll / SphereCastAll method that can detect multiple hits on the same mesh and I ran into something I did not know about CapsuleCasts or SphereCasts and I am not sure if this is an intended behavior.

    From what I understood, all casts (raycast, capsulecast, etc...) would not detect the collider if the casts origin was in the collider. This does not completely seem to be the case for capsulecasts (or SphereCasts).

    This video I made displays the problems of both the CapsuleCast and SphereCast


    Heres the code of the CapsuleCast and SphereCast to show its just a plain Cast with nothing special being done. The code for displaying the Casts is irrelevant, you only really need to focus on the white line produced by this code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestCastsProblems : MonoBehaviour
    5. {
    6.     public bool capsuleNotSphere;
    7.  
    8.     GameObject capsule;
    9.     GameObject sphere;
    10.     float radius = .5f;
    11.     float distance = 100;
    12.     Vector3 castOrigin;
    13.     Vector3 bottomPoint;
    14.     Vector3 topPoint;
    15.  
    16.     void Start()
    17.     {
    18.         Debug.DrawRay(transform.position, transform.forward * distance, Color.blue, 100000);
    19.         Debug.DrawRay(transform.position, transform.up * distance, Color.blue, 100000);
    20.  
    21.         castOrigin = transform.position;
    22.         bottomPoint = castOrigin - (transform.up * radius);
    23.         topPoint = castOrigin + (transform.up * radius);
    24.  
    25.         MeshTestCreate(ref capsule, Vector3.zero, PrimitiveType.Capsule);
    26.         MeshTestCreate(ref sphere, Vector3.zero, PrimitiveType.Sphere);
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         RaycastHit hitInfo;
    32.         if(capsuleNotSphere)
    33.         {
    34.             Physics.CapsuleCast(bottomPoint, topPoint, radius, transform.forward, out hitInfo, distance);
    35.         }else{
    36.             Physics.SphereCast(castOrigin, radius, transform.forward, out hitInfo, distance);
    37.         }
    38.  
    39.         if(hitInfo.collider)
    40.         {
    41.             if(capsuleNotSphere)
    42.             {
    43.                 capsule.transform.position = ExtensionsPhysics.CapsuleCastCenterOnCollision(bottomPoint, topPoint, radius, transform.forward, hitInfo.point);
    44.             }else{
    45.                 sphere.transform.position = ExtensionsPhysics.SphereCastCenterOnCollision(castOrigin, radius, transform.forward, hitInfo.point);
    46.             }
    47.  
    48.             Debug.DrawRay(hitInfo.point, Vector3.up * distance);
    49.         }
    50.     }
    51.  
    52.     void MeshTestCreate(ref GameObject newGameObject, Vector3 position, PrimitiveType type)
    53.     {
    54.         newGameObject = GameObject.CreatePrimitive(type);
    55.         newGameObject.transform.position = position;
    56.         newGameObject.GetComponent<Collider>().enabled = false;
    57.     }
    58. }

    Due to these inconsistencies, how would I be able to make my CapsuleCastAll / SphereCastAll method?

    Any info is appreciated!
     
    Last edited: Mar 9, 2015
    gameDevMode likes this.