Advertisement
shadowx320

RaycastController.cs

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