Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFollow : MonoBehaviour {
  5.  
  6. public Controller2D target;
  7. public float verticalOffset;
  8. public float lookAheadDstX;
  9. public float lookSmoothTimeX;
  10. public float verticalSmoothTime;
  11. public Vector2 focusAreaSize;
  12.  
  13. FocusArea focusArea;
  14.  
  15. float currentLookAheadX;
  16. float targetLookAheadX;
  17. float lookAheadDirX;
  18. float smoothLookVelocityX;
  19. float smoothVelocityY;
  20.  
  21. bool lookAheadStopped;
  22.  
  23. void Start() {
  24. focusArea = new FocusArea (target.collider.bounds, focusAreaSize);
  25. }
  26.  
  27. void LateUpdate() {
  28. focusArea.Update (target.collider.bounds);
  29.  
  30. Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;
  31.  
  32. if (focusArea.velocity.x != 0) {
  33. lookAheadDirX = Mathf.Sign (focusArea.velocity.x);
  34. if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0) {
  35. lookAheadStopped = false;
  36. targetLookAheadX = lookAheadDirX * lookAheadDstX;
  37. }
  38. else {
  39. if (!lookAheadStopped) {
  40. lookAheadStopped = true;
  41. targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX)/4f;
  42. }
  43. }
  44. }
  45.  
  46.  
  47. currentLookAheadX = Mathf.SmoothDamp (currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
  48.  
  49. focusPosition.y = Mathf.SmoothDamp (transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
  50. focusPosition += Vector2.right * currentLookAheadX;
  51. transform.position = (Vector3)focusPosition + Vector3.forward * -10;
  52. }
  53.  
  54. void OnDrawGizmos() {
  55. Gizmos.color = new Color (1, 0, 0, .5f);
  56. Gizmos.DrawCube (focusArea.centre, focusAreaSize);
  57. }
  58.  
  59. struct FocusArea {
  60. public Vector2 centre;
  61. public Vector2 velocity;
  62. float left,right;
  63. float top,bottom;
  64.  
  65.  
  66. public FocusArea(Bounds targetBounds, Vector2 size) {
  67. left = targetBounds.center.x - size.x/2;
  68. right = targetBounds.center.x + size.x/2;
  69. bottom = targetBounds.min.y;
  70. top = targetBounds.min.y + size.y;
  71.  
  72. velocity = Vector2.zero;
  73. centre = new Vector2((left+right)/2,(top +bottom)/2);
  74. }
  75.  
  76. public void Update(Bounds targetBounds) {
  77. float shiftX = 0;
  78. if (targetBounds.min.x < left) {
  79. shiftX = targetBounds.min.x - left;
  80. } else if (targetBounds.max.x > right) {
  81. shiftX = targetBounds.max.x - right;
  82. }
  83. left += shiftX;
  84. right += shiftX;
  85.  
  86. float shiftY = 0;
  87. if (targetBounds.min.y < bottom) {
  88. shiftY = targetBounds.min.y - bottom;
  89. } else if (targetBounds.max.y > top) {
  90. shiftY = targetBounds.max.y - top;
  91. }
  92. top += shiftY;
  93. bottom += shiftY;
  94. centre = new Vector2((left+right)/2,(top +bottom)/2);
  95. velocity = new Vector2 (shiftX, shiftY);
  96. }
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement