Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(BoxCollider2D))]
  5. public class RaycastController : MonoBehaviour
  6. {
  7.     public LayerMask collisionMask;
  8.  
  9.     public const float skinWidth = .015f;
  10.     public int horizontalRayCount = 4;
  11.     public int verticalRayCount = 4;
  12.  
  13.     [HideInInspector]
  14.     public float horizontalRaySpacing;
  15.     [HideInInspector]
  16.     public float verticalRaySpacing;
  17.  
  18.     [HideInInspector]
  19.     public BoxCollider2D theCollider;
  20.     public RaycastOrigins raycastOrigins;
  21.  
  22.     public virtual void Awake()
  23.     {
  24.         theCollider = GetComponent<BoxCollider2D>();
  25.     }
  26.  
  27.     public virtual void Start()
  28.     {
  29.         CalculateRaySpacing();
  30.     }
  31.  
  32.     public void UpdateRaycastOrigins()
  33.     {
  34.         Bounds bounds = theCollider.bounds;
  35.         bounds.Expand(skinWidth * -2);
  36.  
  37.         raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
  38.         raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
  39.         raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
  40.         raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
  41.     }
  42.  
  43.     public void CalculateRaySpacing()
  44.     {
  45.         Bounds bounds = theCollider.bounds;
  46.         bounds.Expand(skinWidth * -2);
  47.  
  48.         horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue);
  49.         verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue);
  50.  
  51.         horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
  52.         verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
  53.     }
  54.  
  55.     public struct RaycastOrigins
  56.     {
  57.         public Vector2 topLeft, topRight;
  58.         public Vector2 bottomLeft, bottomRight;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement