Advertisement
Guest User

Untitled

a guest
Jun 16th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. Vector3 gravityUp = myPlayerGO.transform.position - new Vector3(0,0,0);
  2. gravityUp.Normalize();
  3. myRigidbody.AddForce(gravityUp * Gravity * myRigidbody.mass);
  4. if(Grounded >= 1)
  5. myRigidbody.drag = 0.1f;
  6. else
  7. myRigidbody.drag = 1.0f;
  8.  
  9.  
  10. Quaternion quatern = Quaternion.FromToRotation(myPlayerGO.transform.up, gravityUp);
  11. quatern = quatern * myPlayerGO.transform.rotation;
  12. myPlayerGO.transform.rotation = Quaternion.Slerp(myPlayerGO.transform.rotation, quatern, 0.1f);
  13.  
  14.  
  15.  
  16. if(myRigidbody.velocity.magnitude < MoveSpeed() && Grounded >= 1)
  17. {
  18. myRigidbody.AddForce(myPlayerGO.transform.forward * VAxis * MoveSpeed());
  19. myRigidbody.AddForce(myPlayerGO.transform.right * HAxis * MoveSpeed());
  20. }
  21. if(Jumping == true && Grounded >= 1)
  22. {
  23. myRigidbody.velocity = myRigidbody.velocity + (myRigidbody.transform.up * JumpSpeed);
  24. Jumping = false;
  25. }
  26.  
  27. if(PlayerIsCombative || (!PlayerIsCombative && MouseButton2))
  28.  
  29. {
  30.  
  31.  
  32. MouseX += MouseXAxis * 5f;
  33. MouseY -= MouseYAxis * 5f;
  34. if (MouseY < -360) MouseY += 360;
  35. if (MouseY > 360) MouseY -= 360;
  36. //MouseY = Mathf.Clamp(MouseY, -40f, 80f);
  37. }
  38. if(MouseScrollAxis < -0.01f || MouseScrollAxis > 0.01f)
  39. {
  40. desiredDistance = Mathf.Clamp(Distance - MouseScrollAxis * 5f,0.5f,5f);
  41. preOccludedDistance = desiredDistance;
  42. distanceSmooth = 0.05f;
  43. }
  44. int count = 0;
  45. do{
  46. if(desiredDistance < preOccludedDistance)
  47. {
  48. Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
  49. float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
  50. if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
  51. desiredDistance = preOccludedDistance;
  52. }
  53. Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
  54. desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
  55. count++;
  56. }while(CheckIfOccluded(count));
  57. CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
  58. float posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, 0.05f);
  59. float posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, 0.1f);
  60. float posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, 0.05f);
  61. position = new Vector3(posX, posY, posZ);
  62. myPlayerCameraGO.transform.localPosition = position;
  63. Vector3 relPlayerPosition = CameraLookAt.transform.position - myPlayerCameraGO.transform.position;
  64. myPlayerCameraGO.transform.rotation = Quaternion.LookRotation(relPlayerPosition, myPlayerGO.transform.up);
  65.  
  66.  
  67.  
  68.  
  69. //Vector3 v = myPlayerCameraGO.transform.forward;
  70. //Quaternion q = Quaternion.LookRotation(v, myPlayerGO.transform.up);
  71. //q.eulerAngles = new Vector3(0,q.eulerAngles.y,0);
  72. //myPlayerGO.transform.rotation = Quaternion.LookRotation(myPlayerCameraGO.transform.forward, myPlayerGO.transform.up);
  73.  
  74. //myPlayerGO.transform.eulerAngles = new Vector3(0,myPlayerCameraGO.transform.eulerAngles.y,0);
  75. //myPlayerGO.transform.forward = myPlayerCameraGO.transform.TransformDirection(myPlayerCameraGO.transform.forward);
  76.  
  77.  
  78. ////////////////////////////////////////////////////////////////////////////
  79.  
  80. private Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
  81. {
  82. Vector3 direction = new Vector3(0, 0, -distance);
  83. Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
  84. return CameraLookAt.transform.localPosition + rotation * direction;
  85. }
  86. private bool CheckIfOccluded(int count)
  87. {
  88. bool isOccluded = false;
  89. float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
  90. if(nearestDistance != -1)
  91. {
  92. if(count < 10)
  93. {
  94. isOccluded = true;
  95. Distance -= 0.05f;
  96. if(Distance < 0.25f)
  97. Distance = 0.25f;
  98. }
  99. else
  100. Distance = nearestDistance - myPlayerCamera.nearClipPlane;
  101. desiredDistance = Distance;
  102. distanceSmooth = 1f;
  103. }
  104. return isOccluded;
  105. }
  106. private float CheckCameraPoints(Vector3 from, Vector3 to)
  107. {
  108. var nearDistance = -1f;
  109. RaycastHit hitInfo;
  110. ClipPlanePoints clipPlanePoints = ClipPlaneAtNear( to);
  111. if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
  112. nearDistance = hitInfo.distance;
  113. if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
  114. if(hitInfo.distance < nearDistance || nearDistance == -1)
  115. nearDistance = hitInfo.distance;
  116. if(Physics.Linecast(from, clipPlanePoints.UpperRight, out hitInfo) && hitInfo.collider.tag != "Player")
  117. if(hitInfo.distance < nearDistance || nearDistance == -1)
  118. nearDistance = hitInfo.distance;
  119. if(Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player")
  120. if(hitInfo.distance < nearDistance || nearDistance == -1)
  121. nearDistance = hitInfo.distance;
  122. if(Physics.Linecast(from, to + transform.forward * -myPlayerCamera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
  123. if(hitInfo.distance < nearDistance || nearDistance == -1)
  124. nearDistance = hitInfo.distance;
  125. return nearDistance;
  126. }
  127. private static ClipPlanePoints ClipPlaneAtNear(Vector3 pos)
  128. {
  129. ClipPlanePoints clipPlanePoints = new ClipPlanePoints();
  130. Transform transform = myPlayerCameraGO.transform;
  131. float HalfFOV = (myPlayerCamera.fieldOfView / 2) * Mathf.Deg2Rad;
  132. float aspect = myPlayerCamera.aspect;
  133. float distance = myPlayerCamera.nearClipPlane;
  134. float height = distance * Mathf.Tan(HalfFOV);
  135. float width = height * aspect;
  136. clipPlanePoints.LowerRight = pos + transform.right * width;
  137. clipPlanePoints.LowerRight -= transform.up * height;
  138. clipPlanePoints.LowerRight += transform.forward * distance;
  139. clipPlanePoints.LowerLeft = pos - transform.right * width;
  140. clipPlanePoints.LowerLeft -= transform.up * height;
  141. clipPlanePoints.LowerLeft += transform.forward * distance;
  142. clipPlanePoints.UpperRight = pos + transform.right * width;
  143. clipPlanePoints.UpperRight += transform.up * height;
  144. clipPlanePoints.UpperRight += transform.forward * distance;
  145. clipPlanePoints.UpperLeft = pos - transform.right * width;
  146. clipPlanePoints.UpperLeft += transform.up * height;
  147. clipPlanePoints.UpperLeft += transform.forward * distance;
  148. return clipPlanePoints;
  149. }
  150. private struct ClipPlanePoints
  151. {
  152. public Vector3 UpperLeft;
  153. public Vector3 LowerLeft;
  154. public Vector3 UpperRight;
  155. public Vector3 LowerRight;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement