Advertisement
shadowx320

Controller2D.cs

Sep 27th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Controller2D : RaycastController
  5. {
  6.  
  7. float maxClimbAngle = 80;
  8. float maxDescendAngle = 80;
  9.  
  10. public CollisionInfo collisions;
  11.  
  12.  
  13. public override void Start()
  14. {
  15. base.Start();
  16.  
  17. }
  18.  
  19. public void Move(Vector3 velocity)
  20. {
  21. UpdateRaycastOrigins();
  22. collisions.Reset();
  23. collisions.velocityOld = velocity;
  24.  
  25. if (velocity.y < 0)
  26. {
  27. DescendSlope(ref velocity);
  28. }
  29. if (velocity.x != 0)
  30. {
  31. HorizontalCollisions(ref velocity);
  32. }
  33. if (velocity.y != 0)
  34. {
  35. VerticalCollisions(ref velocity);
  36. }
  37.  
  38. transform.Translate(velocity);
  39. }
  40.  
  41. void HorizontalCollisions(ref Vector3 velocity)
  42. {
  43. float directionX = Mathf.Sign(velocity.x);
  44. float rayLength = Mathf.Abs(velocity.x) + skinWidth;
  45.  
  46. for (int i = 0; i < horizontalRayCount; i++)
  47. {
  48. Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
  49. rayOrigin += Vector2.up * (horizontalRaySpacing * i);
  50. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
  51.  
  52. Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);
  53.  
  54. if (hit)
  55. {
  56.  
  57. float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
  58.  
  59. if (i == 0 && slopeAngle <= maxClimbAngle)
  60. {
  61. if (collisions.descendingSlope)
  62. {
  63. collisions.descendingSlope = false;
  64. velocity = collisions.velocityOld;
  65. }
  66. float distanceToSlopeStart = 0;
  67. if (slopeAngle != collisions.slopeAngleOld)
  68. {
  69. distanceToSlopeStart = hit.distance - skinWidth;
  70. velocity.x -= distanceToSlopeStart * directionX;
  71. }
  72. ClimbSlope(ref velocity, slopeAngle);
  73. velocity.x += distanceToSlopeStart * directionX;
  74. }
  75.  
  76. if (!collisions.climbingSlope || slopeAngle > maxClimbAngle)
  77. {
  78. velocity.x = (hit.distance - skinWidth) * directionX;
  79. rayLength = hit.distance;
  80.  
  81. if (collisions.climbingSlope)
  82. {
  83. velocity.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x);
  84. }
  85.  
  86. collisions.left = directionX == -1;
  87. collisions.right = directionX == 1;
  88. }
  89. }
  90. }
  91. }
  92.  
  93. void VerticalCollisions(ref Vector3 velocity)
  94. {
  95. float directionY = Mathf.Sign(velocity.y);
  96. float rayLength = Mathf.Abs(velocity.y) + skinWidth;
  97.  
  98. for (int i = 0; i < verticalRayCount; i++)
  99. {
  100. Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
  101. rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
  102. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
  103.  
  104. Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
  105.  
  106. if (hit)
  107. {
  108. velocity.y = (hit.distance - skinWidth) * directionY;
  109. rayLength = hit.distance;
  110.  
  111. if (collisions.climbingSlope)
  112. {
  113. velocity.x = velocity.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x);
  114. }
  115.  
  116. collisions.below = directionY == -1;
  117. collisions.above = directionY == 1;
  118. }
  119. }
  120.  
  121. if (collisions.climbingSlope)
  122. {
  123. float directionX = Mathf.Sign(velocity.x);
  124. rayLength = Mathf.Abs(velocity.x) + skinWidth;
  125. Vector2 rayOrigin = ((directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight) + Vector2.up * velocity.y;
  126. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
  127.  
  128. if (hit)
  129. {
  130. float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
  131. if (slopeAngle != collisions.slopeAngle)
  132. {
  133. velocity.x = (hit.distance - skinWidth) * directionX;
  134. collisions.slopeAngle = slopeAngle;
  135. }
  136. }
  137. }
  138. }
  139.  
  140. void ClimbSlope(ref Vector3 velocity, float slopeAngle)
  141. {
  142. float moveDistance = Mathf.Abs(velocity.x);
  143. float climbVelocityY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * moveDistance;
  144.  
  145. if (velocity.y <= climbVelocityY)
  146. {
  147. velocity.y = climbVelocityY;
  148. velocity.x = Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign(velocity.x);
  149. collisions.below = true;
  150. collisions.climbingSlope = true;
  151. collisions.slopeAngle = slopeAngle;
  152. }
  153. }
  154.  
  155. void DescendSlope(ref Vector3 velocity)
  156. {
  157. float directionX = Mathf.Sign(velocity.x);
  158. Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomRight : raycastOrigins.bottomLeft;
  159. RaycastHit2D hit = Physics2D.Raycast(rayOrigin, -Vector2.up, Mathf.Infinity, collisionMask);
  160.  
  161. if (hit)
  162. {
  163. float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
  164. if (slopeAngle != 0 && slopeAngle <= maxDescendAngle)
  165. {
  166. if (Mathf.Sign(hit.normal.x) == directionX)
  167. {
  168. if (hit.distance - skinWidth <= Mathf.Tan(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x))
  169. {
  170. float moveDistance = Mathf.Abs(velocity.x);
  171. float descendVelocityY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * moveDistance;
  172. velocity.x = Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign(velocity.x);
  173. velocity.y -= descendVelocityY;
  174.  
  175. collisions.slopeAngle = slopeAngle;
  176. collisions.descendingSlope = true;
  177. collisions.below = true;
  178. }
  179. }
  180. }
  181. }
  182. }
  183.  
  184.  
  185.  
  186. public struct CollisionInfo
  187. {
  188. public bool above, below;
  189. public bool left, right;
  190.  
  191. public bool climbingSlope;
  192. public bool descendingSlope;
  193. public float slopeAngle, slopeAngleOld;
  194. public Vector3 velocityOld;
  195.  
  196. public void Reset()
  197. {
  198. above = below = false;
  199. left = right = false;
  200. climbingSlope = false;
  201. descendingSlope = false;
  202.  
  203. slopeAngleOld = slopeAngle;
  204. slopeAngle = 0;
  205. }
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement