spacepaste

  1.  
  2. using System;
  3. using UnityEngine;
  4. using UnityStandardAssets.CrossPlatformInput;
  5. namespace UnityStandardAssets.Characters.FirstPerson
  6. {
  7. [RequireComponent(typeof(Rigidbody))]
  8. [RequireComponent(typeof(CapsuleCollider))]
  9. public class JumpingPhysics : MonoBehaviour
  10. {
  11. [Serializable]
  12. public class AdvancedSettings
  13. {
  14. public float groundCheckDistance = 0.01f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
  15. public float stickToGroundHelperDistance = 0.5f; // stops the character
  16. public float slowDownRate = 20f; // rate at which the controller comes to a stop when there is no input
  17. public bool airControl; // can the user control the direction that is being moved in the air
  18. [Tooltip("set it to 0.1 or more if you get stuck in wall")]
  19. public float shellOffset; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
  20. }
  21. private float m_YRotation;
  22. private Rigidbody m_RigidBody;
  23. public bool m_PreviouslyGrounded, m_IsGrounded;
  24. public Vector3 m_GroundContactNormal;
  25. private CapsuleCollider m_Capsule;
  26. public AdvancedSettings advancedSettings = new AdvancedSettings();
  27. public float verticalVelocity = 0f;
  28. public float gravity = 9.80665f;
  29. public float jumpForce = 20.0f;
  30. public float MovementSpeed;
  31. public float MovementSpeedDefault = 10f;
  32. public float forces = 10.0f;
  33. public float drag = 10.0f;
  34. public CharacterController controller;
  35. public float slowdownFactor = 0.005f;
  36. public float Matrix_Time = 0;
  37. public float Time_Original = 0;
  38. public bool Matrix = false;
  39. public bool Matrix_Instant_Time = true;
  40. public int Matrix_State = 0;
  41. public void DoSlowmotion()
  42. {
  43. Matrix_State = 2;
  44. Matrix_Time = slowdownFactor;
  45. Time.timeScale = Matrix_Time;
  46. }
  47. public void DoSpeedUp()
  48. {
  49. Matrix_State = 3;
  50. Matrix_Time += (1f / 1f) * Time.unscaledDeltaTime;
  51. Matrix_Time = Mathf.Clamp(Matrix_Time, 0f, 1f);
  52. Time.timeScale = Matrix_Time;
  53. if (Matrix_Time >= Time_Original) Matrix_State = 0;
  54. }
  55. public void DoSlowDown()
  56. {
  57. Matrix_Time -= (1f / 1f) * Time.unscaledDeltaTime;
  58. Matrix_Time = Mathf.Clamp(Matrix_Time, 0f, 1f);
  59. Time.timeScale = Matrix_Time;
  60. if (Matrix_Time < slowdownFactor) Matrix_State = 1;
  61. }
  62. public void Start()
  63. {
  64. if (Matrix_Time == 0) Matrix_Time = Time.timeScale;
  65. if (Time_Original == 0) Time_Original = Time.timeScale;
  66. MovementSpeed = MovementSpeedDefault;
  67. m_RigidBody = GetComponent<Rigidbody>();
  68. m_Capsule = GetComponent<CapsuleCollider>();
  69. controller = GetComponent<CharacterController>();
  70. }
  71. private Vector3 moveDirection = Vector3.zero;
  72. private void Update()
  73. {
  74. Update1();
  75. }
  76. private void Update1()
  77. {
  78. GroundCheck();
  79. InteractRaycast();
  80. if (controller.isGrounded)
  81. {
  82. moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
  83. moveDirection = transform.TransformDirection(moveDirection);
  84. moveDirection *= MovementSpeed;
  85. if (Input.GetButton("Jump")) moveDirection.y = jumpForce / Matrix_Time;
  86. MovementSpeed = MovementSpeedDefault / Matrix_Time;
  87. }
  88. else
  89. {
  90. moveDirection.y -= (gravity * Time.fixedUnscaledDeltaTime) / Matrix_Time;
  91. }
  92. verticalVelocity = moveDirection.y;
  93. controller.Move(moveDirection * Time.fixedDeltaTime);
  94. if (Input.GetButtonDown("Matrix")) Matrix = !Matrix;
  95. if (Matrix)
  96. {
  97. if (Matrix_Instant_Time)
  98. {
  99. if (Matrix_State == 0) DoSlowmotion();
  100. }
  101. else
  102. {
  103. if (Matrix_State == 0 || Matrix_State == 3) DoSlowDown();
  104. if (Matrix_State == 1) DoSlowmotion();
  105. }
  106. }
  107. else
  108. {
  109. if (Matrix_Instant_Time)
  110. {
  111. Matrix_Time = Time_Original;
  112. Time.timeScale = Matrix_Time;
  113. Matrix_State = 0;
  114. }
  115. else DoSpeedUp();
  116. }
  117. Time.fixedDeltaTime = Time.timeScale * .02f;
  118. }
  119. private Vector2 GetInput()
  120. {
  121. Vector2 input = new Vector2
  122. {
  123. x = CrossPlatformInputManager.GetAxis("Horizontal"),
  124. y = CrossPlatformInputManager.GetAxis("Vertical")
  125. };
  126. return input;
  127. }
  128. /// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
  129. private void GroundCheck()
  130. {
  131. RaycastHit hitInfo;
  132. if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
  133. ((m_Capsule.height / 2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
  134. {
  135. m_IsGrounded = true;
  136. m_GroundContactNormal = hitInfo.normal;
  137. }
  138. else
  139. {
  140. m_IsGrounded = false;
  141. m_GroundContactNormal = Vector3.up;
  142. }
  143. }
  144. /*
  145. terminal velocity,
  146. the mass of the falling object (
  147. In common usage, the mass of an object is often referred to as its weight, though these are in fact different concepts and quantities. In scientific contexts, mass is the amount of "matter" in an object, whereas weight is the force exerted on an object by gravity.
  148. mass (an intrinsic property of an object)
  149. vs weight (an object's resistance to deviating from its natural course of free fall, which can be influenced by the nearby gravitational field. No matter how strong the gravitational field, objects in free fall are weightless)
  150. ), ( no calculation for this )
  151. the acceleration due to gravity (
  152. Near Earth's surface, gravitational acceleration is approximately 9.8 m/s2, which means that, ignoring the effects of air resistance, the speed of an object falling freely will increase by about 9.8 metres per second every second
  153. The precise strength of Earth's gravity varies depending on location. The nominal "average" value at Earth's surface, known as standard gravity is, by definition, 9.80665 m/s2
  154. Gravity decreases with altitude as one rises above the Earth's surface because greater altitude means greater distance from the Earth's centre
  155. (
  156. the gravitational acceleration at height h above sea level,
  157. the Earth's mean radius (
  158. Earth radius is the distance from a selected center of Earth to a point on its surface, which is often chosen to be sea level.
  159. ),
  160. the standard gravitational acceleration (
  161. The standard acceleration due to gravity, sometimes abbreviated as standard gravity, usually denoted by ɡ0 or ɡn, is the nominal gravitational acceleration of an object in a vacuum near the surface of the Earth. It is defined by standard as 9.80665 m/s2.
  162. )
  163. )
  164. ),
  165. the drag coefficient (
  166. drag force, which is by definition the force component in the direction of the flow velocity (
  167. for force:
  168. In physics, a force is any interaction that, when unopposed, will change the motion of an object.
  169. (numourous types of force and force equations for this)
  170. for flow velocity:
  171. (the drag coefficient -> the flow velocity relative to the object)
  172. for drag force:
  173. (numourous types of drag and drag equations for this)
  174. ),
  175. the mass density of the fluid (
  176. The density, or more precisely, the volumetric mass density, of a substance is its mass per unit volume.
  177. density = mass (the mass of the falling object) / volume (
  178. Volume is the quantity of three-dimensional space enclosed by a closed surface
  179. ( calculation dependant of shape of object )
  180. )
  181. ),
  182. the flow velocity relative to the object (
  183. In continuum mechanics the macroscopic velocity, also flow velocity in fluid dynamics or drift velocity in electromagnetism, is a vector field used to mathematically describe the motion of a continuum.
  184. flow velocity u = u(t,x)
  185. velocity of an element of fluid at position x and time t
  186. flow speed q = ||u|| (scalar field)
  187. ),
  188. the reference area (
  189. Area is the quantity that expresses the extent of a two-dimensional figure or shape, or planar lamina, in the plane. Surface area is its analog on the two-dimensional surface of a three-dimensional object.
  190. ( calculation dependant of shape of object )
  191. )
  192. ),
  193. the density of the fluid through which the object is falling (
  194. the drag coefficient -> the mass density of the fluid
  195. ),
  196. the projected area of the object (
  197. Projected area is two-dimensional area measurement of a three-dimensional object by projecting its shape on to an arbitrary plane.
  198. The geometrical definition of a projected area is: "the rectilinear parallel projection of a surface of any shape onto a plane"
  199. ( calculation dependant of shape of object )
  200. )
  201. */
  202. public float drag_calc(float mass_density, float flow_velocity, float reference_area, float drag_coefficient)
  203. {
  204. return (1f / 2f) * mass_density * (flow_velocity * flow_velocity) * drag_coefficient * reference_area;
  205. }
  206. void InteractRaycast()
  207. {
  208. Vector3 playerPosition = transform.position;
  209. Vector3 forwardDirection = transform.forward;
  210. Ray InteractionRay = new Ray(playerPosition, forwardDirection);
  211. RaycastHit InteractionRayHit;
  212. float InteractionRayLength = 5.0f;
  213. Vector3 InteractionRayEndpoint = forwardDirection * InteractionRayLength;
  214. Debug.DrawLine(playerPosition, InteractionRayEndpoint);
  215. bool hitfound = Physics.Raycast(InteractionRay, out InteractionRayHit, InteractionRayLength);
  216. if (hitfound)
  217. {
  218. GameObject hitGameObject = InteractionRayHit.transform.gameObject;
  219. string hitFeedBack = hitGameObject.name;
  220. printfTools.Tools.fprintf(Debug.Log, "FPRINTF raycast hit object with name %s", hitFeedBack);
  221. }
  222. }
  223. }
  224. }
  225.