SHARE
TWEET

Example

a guest Aug 5th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(CapsuleCollider))]
  4. public class CapsuleCastCheck : MonoBehaviour
  5. {
  6.     private CapsuleCollider capsule = null;
  7.  
  8.     [SerializeField]
  9.     private float checkAngle = 0f;
  10.  
  11.     [SerializeField]
  12.     private float checkDist = 1f;
  13.  
  14.     private void OnDrawGizmosSelected()
  15.     {
  16.         if (capsule == null)
  17.             capsule = GetComponent<CapsuleCollider>();
  18.  
  19.         // incorrect
  20.         //Vector3 pointOffset = Vector3.up * (capsule.height / 2f);
  21.  
  22.         // correct
  23.         Vector3 pointOffset = Vector3.up * (capsule.height / 2f - capsule.radius);
  24.  
  25.         Vector3 point1 = transform.TransformPoint(pointOffset);
  26.         Vector3 point2 = transform.TransformPoint(-pointOffset);
  27.  
  28.         Vector3 checkDir = Quaternion.AngleAxis(checkAngle, transform.up) * transform.forward;
  29.  
  30.         bool hit = Physics.CapsuleCast(point1, point2, capsule.radius, checkDir, checkDist);
  31.  
  32.         Gizmos.color = Color.gray;
  33.         Gizmos.DrawWireSphere(transform.TransformPoint(pointOffset), capsule.radius);
  34.         Gizmos.DrawWireSphere(transform.TransformPoint(-pointOffset), capsule.radius);
  35.  
  36.         Gizmos.color = hit ? Color.green : Color.red;
  37.         Gizmos.DrawWireSphere(transform.TransformPoint(pointOffset) + checkDir, capsule.radius);
  38.         Gizmos.DrawWireSphere(transform.TransformPoint(-pointOffset) + checkDir, capsule.radius);
  39.     }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top