Advertisement
Guest User

Kappa

a guest
Sep 1st, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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. [HideInInspector]
  14. public float horizontalRaySpacing;
  15. [HideInInspector]
  16. public float verticalRaySpacing;
  17.  
  18. [HideInInspector]
  19. public BoxCollider2D collider;
  20. public RaycastOrigins raycastOrigins;
  21.  
  22. public virtual void Awake() {
  23. collider = GetComponent<BoxCollider2D> ();
  24. }
  25.  
  26. public virtual void Start() {
  27. CalculateRaySpacing ();
  28. }
  29.  
  30. public void UpdateRaycastOrigins() {
  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. Bounds bounds = collider.bounds;
  42. bounds.Expand (skinWidth * -2);
  43.  
  44. horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
  45. verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);
  46.  
  47. horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
  48. verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
  49. }
  50.  
  51. public struct RaycastOrigins {
  52. public Vector2 topLeft, topRight;
  53. public Vector2 bottomLeft, bottomRight;
  54. }
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. using UnityEngine;
  93. using System.Collections;
  94.  
  95. [RequireComponent (typeof (Controller2D))]
  96. public class Player : MonoBehaviour {
  97. public float jumpHeight = 4;
  98. public float timeToJumpApex = .4f;
  99.  
  100. float accelerationTimeAirborne = .2f;
  101. float accelerationTimeGrounded = .1f;
  102. float moveSpeed = 6;
  103.  
  104. float gravity;
  105. float jumpVelocity;
  106. Vector3 velocity;
  107. float velocityXSmoothing;
  108.  
  109. Controller2D controller;
  110.  
  111. void Start() {
  112. controller = GetComponent<Controller2D> ();
  113.  
  114. gravity = -(2 * jumpHeight) / Mathf.Pow (timeToJumpApex, 2);
  115. jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
  116. print ("Gravity: " + gravity + "Jump Velocity: " + jumpVelocity);
  117. }
  118.  
  119. void Update() {
  120.  
  121. if (controller.collisions.above || controller.collisions.below) {
  122. velocity.y = 0;
  123. }
  124.  
  125. Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
  126.  
  127. if (Input.GetKeyDown (KeyCode.Space) && controller.collisions.below) {
  128. velocity.y = jumpVelocity;
  129. }
  130. float targetVelocityX = input.x * moveSpeed;
  131. velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
  132. velocity.y += gravity * Time.deltaTime;
  133. controller.Move (velocity * Time.deltaTime);
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement