Advertisement
Guest User

Untitled

a guest
May 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace ProjectX
  5. {
  6. public class MovePlayerCharacter : StateAction
  7. {
  8. PlayerStateManager states;
  9.  
  10.  
  11.  
  12. public MovePlayerCharacter(PlayerStateManager playerStateManager)
  13. {
  14. states = playerStateManager;
  15. }
  16.  
  17. public override bool Execute()
  18. {
  19. states.isGrounded = OnGround();
  20.  
  21. HandleMovement();
  22.  
  23.  
  24. #region OldMovement
  25. /*
  26. float frontY = 0;
  27. RaycastHit hit;
  28. Vector3 targetVelocity = Vector3.zero;
  29.  
  30. if (states.lockOn)
  31. {
  32. targetVelocity = states.mTransform.forward * states.vertical * states.movementSpeed;
  33. targetVelocity += states.mTransform.right * states.horizontal * states.movementSpeed;
  34. }
  35. else
  36. {
  37. targetVelocity = states.mTransform.forward * states.moveAmount * states.movementSpeed;
  38. }
  39.  
  40. Vector3 origin = states.mTransform.position + (targetVelocity.normalized * states.frontRayOffset);
  41. origin.y += .5f;
  42. Debug.DrawRay(origin, -Vector3.up, Color.red, .01f, false);
  43. if (Physics.Raycast(origin, -Vector3.up, out hit, 1, states.ignoreForGroundCheck))
  44. {
  45. float y = hit.point.y;
  46. frontY = y - states.mTransform.position.y;
  47. }
  48.  
  49. Vector3 currentVelocity = states.rigidbody.velocity;
  50.  
  51. //if (states.isLockingOn)
  52. //{
  53. // targetVelocity = states.rotateDirection * states.moveAmount * movementSpeed;
  54. //}
  55.  
  56.  
  57. if (states.isGrounded)
  58. {
  59. float moveAmount = states.moveAmount;
  60.  
  61. if (moveAmount > 0.1f)
  62. {
  63. states.rigidbody.isKinematic = false;
  64. states.rigidbody.drag = 0;
  65. if (Mathf.Abs(frontY) > 0.02f)
  66. {
  67. targetVelocity.y = ((frontY > 0) ? frontY + 0.2f : frontY - 0.2f) * states.movementSpeed;
  68. }
  69. }
  70. else
  71. {
  72. float abs = Mathf.Abs(frontY);
  73.  
  74. if (abs > 0.02f)
  75. {
  76. states.rigidbody.isKinematic = true;
  77. targetVelocity.y = 0;
  78. states.rigidbody.drag = 4;
  79. }
  80.  
  81. }
  82.  
  83. HandleRotation();
  84. }
  85. else
  86. {
  87. //states.collider.height = colStartHeight;
  88. states.rigidbody.isKinematic = false;
  89. states.rigidbody.drag = 0;
  90. targetVelocity.y = currentVelocity.y;
  91. }
  92.  
  93. HandleAnimations();
  94.  
  95. Debug.DrawRay((states.mTransform.position + Vector3.up * .2f), targetVelocity, Color.green, 0.01f, false);
  96. states.rigidbody.velocity = targetVelocity;
  97.  
  98. // Debug.Log(targetVelocity);
  99. //states.rigidbody.velocity = Vector3.Lerp(currentVelocity, targetVelocity, states.delta * states.adaptSpeed);
  100. */
  101. #endregion OldMovement
  102.  
  103. return false;
  104. }
  105.  
  106.  
  107. void HandleMovement()
  108. {
  109. Vector3 axisY = states.transform.forward;
  110. Vector3 axisX = states.transform.right;
  111.  
  112. Vector3 targetVelocity = Vector3.zero;
  113.  
  114. if (states.isGrounded)
  115. {
  116. if (states.vertical != 0 || states.horizontal != 0)
  117. {
  118. if (states.lockOn)
  119. {
  120. axisY = states.vertical * states.transform.forward;
  121. axisX = states.horizontal * states.transform.right;
  122.  
  123. states.rigidbody.isKinematic = false;
  124. states.rigidbody.drag = 0;
  125. }
  126. else
  127. {
  128. axisY = states.vertical * states.transform.forward;
  129. axisX = states.horizontal * states.transform.right;
  130.  
  131. states.rigidbody.isKinematic = false;
  132. states.rigidbody.drag = 0;
  133. }
  134. }
  135. else
  136. {
  137. states.rigidbody.isKinematic = true;
  138. states.rigidbody.drag = 4;
  139. }
  140.  
  141. HandleRotation();
  142. HandleTargetSpeed();
  143. }
  144. else
  145. {
  146. //states.collider.height = colStartHeight;
  147. states.rigidbody.isKinematic = false;
  148. states.rigidbody.drag = 0;
  149. }
  150.  
  151. states.rootMovement = (axisY + axisX).normalized;
  152. targetVelocity = states.rootMovement * states.movementSpeed * (states.targetSpeed);
  153.  
  154. HandleAnimations();
  155.  
  156. Debug.DrawRay((states.mTransform.position + Vector3.up * .2f), targetVelocity, Color.green, 0.01f, false);
  157. states.rigidbody.velocity = targetVelocity;
  158.  
  159. }
  160.  
  161. void HandleTargetSpeed()
  162. {
  163. if (states.isGrounded)
  164. {
  165. if (states.isRunning && states.vertical != 0 && states.vertical > 0)
  166. {
  167. if (states.targetSpeed < 2)
  168. {
  169. states.targetSpeed = states.targetSpeed + 2.5f * Time.deltaTime;
  170. }
  171. else
  172. {
  173. states.targetSpeed = 2;
  174. }
  175. }
  176. else
  177. {
  178. if (states.targetSpeed > 1 && states.vertical > 0)
  179. {
  180. states.targetSpeed = states.targetSpeed - 2f * Time.deltaTime;
  181. }
  182. else
  183. {
  184. states.targetSpeed = 1;
  185. }
  186. }
  187. }
  188. }
  189.  
  190. void HandleRotation()
  191. {
  192. Vector3 targetDir = Vector3.zero;
  193. float moveOverride = states.moveAmount;
  194. if (states.lockOn)
  195. {
  196. targetDir = states.target.position - states.mTransform.position;
  197. moveOverride = 1;
  198. }
  199. else
  200. {
  201. targetDir = states.camera.transform.forward;
  202. }
  203.  
  204. targetDir.Normalize();
  205. targetDir.y = 0;
  206. if (targetDir == Vector3.zero)
  207. targetDir = states.mTransform.forward;
  208.  
  209. Quaternion tr = Quaternion.LookRotation(targetDir);
  210. Quaternion targetRotation = Quaternion.Slerp(
  211. states.mTransform.rotation, tr,
  212. states.delta * moveOverride * states.rotationSpeed);
  213.  
  214. states.mTransform.rotation = targetRotation;
  215.  
  216.  
  217. #region OldStuff
  218. /*
  219. Vector3 targetDir = Vector3.zero;
  220. float moveOverride = states.moveAmount;
  221. if (states.lockOn)
  222. {
  223. targetDir = states.target.position - states.mTransform.position;
  224. moveOverride = 1;
  225. }
  226. else
  227. {
  228. float h = states.horizontal;
  229. float v = states.vertical;
  230.  
  231. targetDir = states.camera.transform.forward * v;
  232. targetDir += states.camera.transform.right * h;
  233. }
  234.  
  235. targetDir.Normalize();
  236. targetDir.y = 0;
  237. if (targetDir == Vector3.zero)
  238. targetDir = states.mTransform.forward;
  239.  
  240. Quaternion tr = Quaternion.LookRotation(targetDir);
  241. Quaternion targetRotation = Quaternion.Slerp(
  242. states.mTransform.rotation, tr,
  243. states.delta * moveOverride * states.rotationSpeed);
  244.  
  245. states.mTransform.rotation = targetRotation;
  246. */
  247. #endregion OldStuff
  248. }
  249.  
  250. void HandleAnimations()
  251. {
  252. if (states.isGrounded)
  253. {
  254. if (states.lockOn)
  255. {
  256. states.anim.SetFloat(StaticStrings.vertical, states.vertical);
  257. states.anim.SetFloat(StaticStrings.horizontal, states.horizontal * states.targetSpeed);
  258. }
  259. else
  260. {
  261. states.anim.SetFloat(StaticStrings.vertical, states.vertical * states.targetSpeed);
  262. states.anim.SetFloat(StaticStrings.horizontal, states.horizontal);
  263. }
  264. }
  265. else
  266. {
  267.  
  268. }
  269. }
  270.  
  271.  
  272. bool OnGround()
  273. {
  274. bool retVal = false;
  275.  
  276. Vector3 origin = states.mTransform.position;
  277. origin.y += .7f;
  278. Vector3 dir = -Vector3.up;
  279. float dis = 1.4f;
  280. RaycastHit hit;
  281. Debug.DrawRay(origin, -Vector3.up * dis, Color.red);
  282. //Debug.DrawRay(origin, dir * dis);
  283. if (Physics.Raycast(origin, dir, out hit, dis, states.ignoreForGroundCheck))
  284. {
  285. retVal = true;
  286. Vector3 targetPosition = hit.point;
  287. states.mTransform.position = targetPosition;
  288. }
  289.  
  290.  
  291. return retVal;
  292. }
  293.  
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement