/Scripts/WOWCamera.js

http://acid-and-base.googlecode.com/ · JavaScript · 153 lines · 102 code · 35 blank · 16 comment · 14 complexity · 20c971bbb048c2bf662e72fe8908dc99 MD5 · raw file

  1. //WowCamera.js
  2. //When the camera hits something, it "instantly" gets behind the player.
  3. //Credits to this script: "matrix211v1"
  4. //found at: http://forum.unity3d.com/threads/16949-WOW-Camera-Movement/page2
  5. var target : Transform;
  6. var targetHeight = 12.0;
  7. var distance = 5.0;
  8. var maxDistance = 20;
  9. var minDistance = 2.5;
  10. var xSpeed = 250.0;
  11. var ySpeed = 120.0;
  12. var yMinLimit = -20;
  13. var yMaxLimit = 80;
  14. var zoomRate = 20;
  15. var rotationDampening = 3.0;
  16. var theta2 : float = 0.5;
  17. private var x = 0.0;
  18. private var y = 0.0;
  19. private var fwd = new Vector3();
  20. private var rightVector = new Vector3();
  21. private var upVector = new Vector3();
  22. private var movingVector = new Vector3();
  23. private var collisionVector = new Vector3();
  24. private var isColliding : boolean = false;
  25. private var a1 = new Vector3();
  26. private var b1 = new Vector3();
  27. private var c1 = new Vector3();
  28. private var d1 = new Vector3();
  29. private var e1 = new Vector3();
  30. private var f1 = new Vector3();
  31. private var h1 = new Vector3();
  32. private var i1 = new Vector3();
  33. @script AddComponentMenu("Camera-Control/WoW Camera")
  34. function Start () {
  35. var angles = transform.eulerAngles;
  36. x = angles.y;
  37. y = angles.x;
  38. // Make the rigid body not change rotation
  39. if (rigidbody)
  40. rigidbody.freezeRotation = true;
  41. }
  42. function LateUpdate () {
  43. if(!target)
  44. return;
  45. // If either mouse buttons are down, let them govern camera position
  46. if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  47. {
  48. x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  49. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  50. // otherwise, ease behind the target if any of the directional keys are pressed
  51. } else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
  52. var targetRotationAngle = target.eulerAngles.y;
  53. var currentRotationAngle = transform.eulerAngles.y;
  54. x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
  55. }
  56. distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
  57. distance = Mathf.Clamp(distance, minDistance, maxDistance);
  58. y = ClampAngle(y, yMinLimit, yMaxLimit);
  59. var rotation:Quaternion = Quaternion.Euler(y, x, 0);
  60. var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
  61. // Check to see if we have a collision
  62. collisionVector = AdjustLineOfSight(transform.position, position);
  63. // Check Line Of Sight
  64. if (collisionVector != Vector3.zero)
  65. {
  66. Debug.Log("Check Line Of Sight");
  67. a1 = transform.position;
  68. b1 = position;
  69. c1 = AdjustLineOfSight(transform.position, position);
  70. d1 = c1 - a1;
  71. e1 = d1.normalized * -1;
  72. f1 = d1 + e1 * 1;
  73. g1 = f1 + a1;
  74. position = g1;
  75. // check distance player to camera
  76. h1 = position - a1;
  77. if (h1.magnitude < 10)
  78. {
  79. position = a1 - fwd * 4;
  80. //position.y = targetPlayer.y;
  81. theta2 = theta2 + .25;
  82. }
  83. // set new camera distance
  84. h1 = position - a1;
  85. distance = h1.magnitude;
  86. }
  87. // check collision
  88. if (Physics.CheckSphere (position, .5) )
  89. {
  90. a1 = transform.position;
  91. newPosition = a1 - fwd * 4;
  92. //newPosition.y = targetPlayer.y;
  93. theta2 = theta2 + .25;
  94. // set new camera distance
  95. h1 = position - a1;
  96. distance = h1.magnitude;
  97. }
  98. //position = Vector3.Slerp(transform.position, position, Time.deltaTime * 100);
  99. transform.rotation = rotation;
  100. transform.position = position;
  101. }
  102. static function ClampAngle (angle : float, min : float, max : float) {
  103. if (angle < -360)
  104. angle += 360;
  105. if (angle > 360)
  106. angle -= 360;
  107. return Mathf.Clamp (angle, min, max);
  108. }
  109. function AdjustLineOfSight (vecA: Vector3, vecB: Vector3)
  110. {
  111. var hit: RaycastHit;
  112. if (Physics.Linecast (vecA, vecB, hit))
  113. {
  114. Debug.Log("I hit something");
  115. return hit.point;
  116. }
  117. return Vector3.zero;
  118. }