Advertisement
shadowx320

PlatformController.cs

Sep 27th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class PlatformController : RaycastController
  6. {
  7.  
  8. public LayerMask passengerMask;
  9. public Vector3 move;
  10.  
  11. public override void Start()
  12. {
  13. base.Start();
  14. }
  15.  
  16. void Update()
  17. {
  18.  
  19. UpdateRaycastOrigins();
  20.  
  21. Vector3 velocity = move * Time.deltaTime;
  22.  
  23. MovePassengers(velocity);
  24. transform.Translate(velocity);
  25.  
  26. }
  27.  
  28. void MovePassengers(Vector3 velocity)
  29. {
  30. HashSet<Transform> movedPassengers = new HashSet<Transform>();
  31.  
  32. float directionX = Mathf.Sign(velocity.x);
  33. float directionY = Mathf.Sign(velocity.y);
  34.  
  35. // Vertically moving platform
  36. if (velocity.y != 0)
  37. {
  38. float rayLength = Mathf.Abs(velocity.y) + skinWidth;
  39.  
  40. for (int i = 0; i < verticalRayCount; i++)
  41. {
  42. Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
  43. rayOrigin += Vector2.right * (verticalRaySpacing * i);
  44. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, passengerMask);
  45.  
  46. if (hit)
  47. {
  48. if (!movedPassengers.Contains(hit.transform))
  49. {
  50. movedPassengers.Add(hit.transform);
  51. float pushX = (directionY == 1) ? velocity.x : 0;
  52. float pushY = velocity.y - (hit.distance - skinWidth) * directionY;
  53.  
  54. hit.transform.Translate(new Vector3(pushX, pushY));
  55. }
  56. }
  57. }
  58. }
  59.  
  60. // Horizontally moving platform
  61. if (velocity.x != 0)
  62. {
  63. float rayLength = Mathf.Abs(velocity.x) + skinWidth;
  64.  
  65. for (int i = 0; i < horizontalRayCount; i++)
  66. {
  67. Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
  68. rayOrigin += Vector2.up * (horizontalRaySpacing * i);
  69. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, passengerMask);
  70.  
  71. if (hit)
  72. {
  73. if (!movedPassengers.Contains(hit.transform))
  74. {
  75. movedPassengers.Add(hit.transform);
  76. float pushX = velocity.x - (hit.distance - skinWidth) * directionX;
  77. float pushY = 0;
  78.  
  79. hit.transform.Translate(new Vector3(pushX, pushY));
  80. }
  81. }
  82. }
  83. }
  84.  
  85. // Passenger on top of a horizontally or downward moving platform
  86. if (directionY == -1 || velocity.y == 0 && velocity.x != 0)
  87. {
  88. float rayLength = skinWidth * 2;
  89.  
  90. for (int i = 0; i < verticalRayCount; i++)
  91. {
  92. Vector2 rayOrigin = raycastOrigins.topLeft + Vector2.right * (verticalRaySpacing * i);
  93. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up, rayLength, passengerMask);
  94.  
  95. if (hit)
  96. {
  97. if (!movedPassengers.Contains(hit.transform))
  98. {
  99. movedPassengers.Add(hit.transform);
  100. float pushX = velocity.x;
  101. float pushY = velocity.y;
  102.  
  103. hit.transform.Translate(new Vector3(pushX, pushY));
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement