Guest User

Example

a guest
Aug 5th, 2019
109
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×