Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 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. const float dstBetweenRays = .25f;
  11. [HideInInspector]
  12. public int horizontalRayCount;
  13. [HideInInspector]
  14. public int verticalRayCount;
  15.  
  16. [HideInInspector]
  17. public float horizontalRaySpacing;
  18. [HideInInspector]
  19. public float verticalRaySpacing;
  20.  
  21. [HideInInspector]
  22. public BoxCollider2D collider;
  23. public RaycastOrigins raycastOrigins;
  24.  
  25. public virtual void Awake() {
  26. collider = GetComponent<BoxCollider2D> ();
  27. }
  28.  
  29. public virtual void Start() {
  30. CalculateRaySpacing ();
  31. }
  32.  
  33. public void UpdateRaycastOrigins() {
  34. Bounds bounds = collider.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. Bounds bounds = collider.bounds;
  45. bounds.Expand (skinWidth * -2);
  46.  
  47. float boundsWidth = bounds.size.x;
  48. float boundsHeight = bounds.size.y;
  49.  
  50. horizontalRayCount = Mathf.RoundToInt (boundsHeight / dstBetweenRays);
  51. verticalRayCount = Mathf.RoundToInt (boundsWidth / dstBetweenRays);
  52.  
  53. horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
  54. verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
  55. }
  56.  
  57. public struct RaycastOrigins {
  58. public Vector2 topLeft, topRight;
  59. public Vector2 bottomLeft, bottomRight;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement