Advertisement
Guest User

Untitled

a guest
Mar 28th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace GP
  6. {
  7. public class PlayerLocoMotion : MonoBehaviour
  8. {
  9. CameraHandler cameraHandler;
  10. PlayerManager playerManager;
  11.  
  12. Transform cameraObject;
  13. InputHandler inputHandler;
  14. public Vector3 moveDirection;
  15.  
  16. PlayerStats playerStats;
  17.  
  18. [HideInInspector]
  19. public Transform myTransform;
  20. [HideInInspector]
  21. public PlayerAnimatorManager animatorHandler;
  22.  
  23. public new Rigidbody rigidbody;
  24. public GameObject normalCamera;
  25.  
  26. [Header("Stats")]
  27. [SerializeField]
  28. float movementSpeed = 1;
  29. public float jogSpeed = 5;
  30. [SerializeField]
  31. float rotationSpeed = 10;
  32.  
  33. public float sprintSpeed = 7;
  34.  
  35. public bool isDodgingForward;
  36. public bool isDodgingBack;
  37. public bool AttackingForceForward;
  38.  
  39. public Transform hips;
  40.  
  41. [Header("Roll Costs")]
  42. public int rollStaminaCost = 15;
  43. public int backstepStaminaCost = 12;
  44.  
  45.  
  46. public CapsuleCollider characterCollider;
  47. public CapsuleCollider characterCollisionBlocker;
  48.  
  49.  
  50. [Header("GroundAndAirDetection")]
  51. public float groundDetectionRayStartPoint = 0.5f;
  52. float minimumDistanceNeededToBeginFall = 1f;
  53. float groundDetectionRayDistance = -0.2f;
  54. LayerMask ignoreForGroundCheck;
  55. public float inAirTimer;
  56. public float fallingSpeed = 45;
  57.  
  58. [Header("SprintCost")]
  59. public int sprintStaminaCost = 1;
  60.  
  61.  
  62. // Start is called before the first frame update
  63. void Start()
  64.  
  65.  
  66. {
  67. Physics.IgnoreCollision(characterCollider, characterCollisionBlocker, true);
  68.  
  69. rigidbody = GetComponent<Rigidbody>();
  70. inputHandler = GetComponent<InputHandler>();
  71. cameraObject = Camera.main.transform;
  72. myTransform = transform;
  73. animatorHandler = GetComponentInChildren<PlayerAnimatorManager>();
  74. animatorHandler.Initialize();
  75. playerManager = GetComponent<PlayerManager>();
  76.  
  77. playerManager.isGrounded = true;
  78. ignoreForGroundCheck = ~(1 << 8 | 1 << 11);
  79. cameraHandler = FindObjectOfType<CameraHandler>();
  80. playerStats = GetComponent<PlayerStats>();
  81.  
  82. }
  83.  
  84. private void Update()
  85. {
  86.  
  87. Debug.Log("Light attack is "+AttackingForceForward);
  88. if (isDodgingForward)
  89. {
  90. float speed = 10;
  91. /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
  92.  
  93. Vector3 vel = rigidbody.velocity;
  94. float drag = -1f;
  95. rigidbody.AddForce(this.transform.forward * 700f * Time.deltaTime, ForceMode.VelocityChange);
  96.  
  97.  
  98. Vector3 v = -rigidbody.velocity;
  99. v.y = 0;
  100.  
  101. Vector3 veloc = v * drag * Time.deltaTime;
  102. Vector3 force = veloc * Time.deltaTime;
  103. float coeff = (1 - Time.deltaTime * drag);
  104.  
  105. rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
  106.  
  107. playerManager.isInteracting = true;
  108. animatorHandler.canRotate = false;
  109. }
  110. if (isDodgingBack)
  111. {
  112. float speed = 10;
  113. /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
  114.  
  115. Vector3 vel = rigidbody.velocity;
  116. float drag = -1f;
  117. rigidbody.AddForce(this.transform.forward * -300f * Time.deltaTime, ForceMode.VelocityChange);
  118.  
  119.  
  120. Vector3 v = -rigidbody.velocity;
  121. v.y = 0;
  122.  
  123. Vector3 veloc = v * drag * Time.deltaTime;
  124. Vector3 force = veloc * Time.deltaTime;
  125. float coeff = (1 - Time.deltaTime * drag);
  126.  
  127. rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
  128.  
  129. playerManager.isInteracting = true;
  130. animatorHandler.canRotate = false;
  131. }
  132. if(AttackingForceForward)
  133. {
  134. AttackForceForward();
  135.  
  136. }
  137.  
  138. }
  139.  
  140. public void AttackForceForward()
  141. {
  142. if(AttackingForceForward)
  143. {
  144. float speed = animatorHandler.playerAttackForce;
  145. /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
  146.  
  147. Vector3 vel = rigidbody.velocity;
  148. float drag = -1f;
  149. rigidbody.AddForce(this.transform.forward * 30f * Time.deltaTime, ForceMode.VelocityChange);
  150.  
  151.  
  152. Vector3 v = -rigidbody.velocity;
  153. v.y = 0;
  154.  
  155. Vector3 veloc = v * drag * Time.deltaTime;
  156. Vector3 force = veloc * Time.deltaTime;
  157. float coeff = (1 - Time.deltaTime * drag);
  158.  
  159. rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
  160.  
  161. playerManager.isInteracting = true;
  162. }
  163.  
  164. }
  165.  
  166. #region Movement
  167. Vector3 normalVector;
  168. Vector3 targetPosition;
  169.  
  170. public void HandleRotation(float delta)
  171. {
  172. if (playerManager.isInteracting == false)
  173. {
  174. if (animatorHandler.canRotate)
  175. {
  176. if (inputHandler.lockOnFlag)
  177. {
  178. if (inputHandler.sprintFlag == true || inputHandler.rollFlag == true)
  179. {
  180. Vector3 targetDirection = Vector3.zero;
  181. targetDirection = cameraHandler.cameraTransform.forward * inputHandler.vertical;
  182. targetDirection += cameraHandler.cameraTransform.right * inputHandler.horizontal;
  183. targetDirection.Normalize();
  184. targetDirection.y = 0;
  185.  
  186. if (targetDirection == Vector3.zero)
  187. {
  188. targetDirection = transform.forward;
  189. }
  190.  
  191. Quaternion tr = Quaternion.LookRotation(targetDirection);
  192. Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, rotationSpeed * Time.deltaTime);
  193.  
  194. transform.rotation = targetRotation;
  195. }
  196. else
  197. {
  198. Vector3 rotationDirection = moveDirection;
  199. rotationDirection = cameraHandler.currentLockOnTarget.transform.position - transform.position;
  200. rotationDirection.y = 0;
  201. rotationDirection.Normalize();
  202. Quaternion tr = Quaternion.LookRotation(rotationDirection);
  203. Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, rotationSpeed * Time.deltaTime);
  204. transform.rotation = targetRotation;
  205. }
  206.  
  207. }
  208. else
  209. {
  210. Vector3 targetDir = Vector3.zero;
  211. float moveOverride = inputHandler.moveAmount;
  212.  
  213. targetDir = cameraObject.forward * inputHandler.vertical;
  214. targetDir += cameraObject.right * inputHandler.horizontal;
  215.  
  216. targetDir.Normalize();
  217. targetDir.y = 0;
  218.  
  219. if (targetDir == Vector3.zero)
  220. {
  221. targetDir = myTransform.forward;
  222. }
  223.  
  224. float rs = rotationSpeed;
  225.  
  226. Quaternion tr = Quaternion.LookRotation(targetDir);
  227. Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
  228.  
  229. myTransform.rotation = targetRotation;
  230. }
  231. }
  232. }
  233.  
  234.  
  235. }
  236.  
  237. public void HandleMovement(float delta)
  238. {
  239.  
  240. if(inputHandler.rollFlag)
  241. {
  242. return;
  243. }
  244. if(playerManager.isInteracting)
  245. {
  246. return;
  247. }
  248.  
  249. if (playerManager.isInteracting == false)
  250. {
  251. moveDirection = cameraObject.forward * inputHandler.vertical;
  252. moveDirection += cameraObject.right * inputHandler.horizontal;
  253. moveDirection.Normalize();
  254. moveDirection.y = 0;
  255.  
  256. float speed = movementSpeed;
  257. Debug.Log(speed);
  258.  
  259. if (inputHandler.sprintFlag && inputHandler.moveAmount > 0.5)
  260. {
  261. speed = sprintSpeed;
  262. playerManager.isSprinting = true;
  263. moveDirection *= speed;
  264. playerStats.TakeStaminaDamage(sprintStaminaCost);
  265.  
  266. }
  267. else if (inputHandler.moveAmount < 0.5)
  268. {
  269. speed = movementSpeed * inputHandler.moveAmount;
  270. moveDirection *= speed;
  271. playerManager.isSprinting = false;
  272. }
  273. else if (inputHandler.moveAmount > 0.5)
  274. {
  275. speed = movementSpeed * inputHandler.moveAmount;
  276. moveDirection *= speed;
  277. playerManager.isSprinting = false;
  278. }
  279.  
  280.  
  281. Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
  282. rigidbody.velocity = projectedVelocity;
  283.  
  284. if (inputHandler.lockOnFlag && inputHandler.sprintFlag == false)
  285. {
  286. animatorHandler.UpdateAnimatorValues(inputHandler.vertical, inputHandler.horizontal, playerManager.isSprinting);
  287. }
  288. else
  289. {
  290. animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0, playerManager.isSprinting);
  291. }
  292.  
  293.  
  294.  
  295. }
  296. else
  297. {
  298. movementSpeed = 0;
  299. }
  300.  
  301. }
  302.  
  303. public void HandleRollingAndSprinting(float delta)
  304. {
  305. if (animatorHandler.anim.GetBool("isInteracting"))
  306. {
  307. return;
  308. }
  309.  
  310. if(playerStats.currentStamina <= 0)
  311. {
  312. return;
  313. }
  314. if (inputHandler.rollFlag)
  315. {
  316.  
  317.  
  318. moveDirection = cameraObject.forward * inputHandler.vertical;
  319. moveDirection += cameraObject.right * inputHandler.horizontal;
  320.  
  321. if (inputHandler.moveAmount > 0)
  322. {
  323. moveDirection = cameraObject.forward * inputHandler.vertical;
  324. moveDirection += cameraObject.right * inputHandler.horizontal;
  325. moveDirection.Normalize();
  326. moveDirection.y = 0;
  327.  
  328. animatorHandler.PlayTargetAnimation("Rolling", true);
  329. Quaternion rollRotation = Quaternion.LookRotation(moveDirection);
  330. myTransform.rotation = rollRotation;
  331. playerStats.TakeStaminaDamage(rollStaminaCost);
  332.  
  333. }
  334. else
  335. {
  336. animatorHandler.PlayTargetAnimation("Backstep", true);
  337. playerStats.TakeStaminaDamage(backstepStaminaCost);
  338. }
  339. }
  340.  
  341.  
  342. }
  343.  
  344. public void HandleFalling(float delta, Vector3 moveDirection)
  345. {
  346. playerManager.isGrounded = false;
  347. RaycastHit hit;
  348. Vector3 orgin = myTransform.position;
  349. orgin.y += groundDetectionRayStartPoint;
  350.  
  351. if(Physics.Raycast(orgin, myTransform.forward, out hit, 0.4f))
  352. {
  353. moveDirection = Vector3.zero;
  354.  
  355. }
  356. if(playerManager.isInAir)
  357. {
  358. rigidbody.AddForce(-Vector3.up * fallingSpeed);
  359. rigidbody.AddForce(moveDirection * fallingSpeed/3);
  360. }
  361.  
  362. Vector3 dir = moveDirection;
  363. dir.Normalize();
  364. orgin = orgin + dir * groundDetectionRayDistance;
  365.  
  366. targetPosition = myTransform.position;
  367.  
  368. Debug.DrawRay(orgin, -Vector3.up * minimumDistanceNeededToBeginFall, Color.red, 0.1f, false);
  369. if(Physics.Raycast(orgin, -Vector3.up, out hit, minimumDistanceNeededToBeginFall, ignoreForGroundCheck))
  370. {
  371. normalVector = hit.normal;
  372. Vector3 tp = hit.point;
  373. playerManager.isGrounded = true;
  374. targetPosition.y = tp.y;
  375.  
  376. if(playerManager.isInAir)
  377. {
  378. if(inAirTimer > 0.5f)
  379. {
  380. Debug.Log("You were in the air for over 0.5f" + inAirTimer);
  381. animatorHandler.PlayTargetAnimation("Land", true);
  382. inAirTimer = 0;
  383. }
  384. else
  385. {
  386. animatorHandler.PlayTargetAnimation("Empty", false);
  387. inAirTimer = 0;
  388. }
  389. playerManager.isInAir = false;
  390. }
  391. }
  392. else
  393. {
  394. if(playerManager.isGrounded)
  395. {
  396. playerManager.isGrounded = false;
  397. }
  398.  
  399. if(playerManager.isInAir == false)
  400. {
  401. if(playerManager.isInteracting == false)
  402. {
  403. animatorHandler.PlayTargetAnimation("Falling", true);
  404. }
  405. Vector3 vel = rigidbody.velocity;
  406. vel.Normalize();
  407. rigidbody.velocity = vel * (movementSpeed / 2);
  408. playerManager.isInAir = true;
  409. }
  410. }
  411. if(playerManager.isInteracting || inputHandler.moveAmount > 0)
  412. {
  413. myTransform.position = Vector3.Lerp(myTransform.position, targetPosition, Time.deltaTime / 0.1f);
  414. }
  415. else
  416. {
  417. myTransform.position = targetPosition;
  418. }
  419.  
  420. if(playerManager.isGrounded)
  421. {
  422. if(playerManager.isInteracting || inputHandler.moveAmount>0)
  423. {
  424. myTransform.position = Vector3.Lerp(myTransform.position, targetPosition, Time.deltaTime);
  425.  
  426. }
  427. else
  428. {
  429. myTransform.position = targetPosition;
  430. }
  431. }
  432.  
  433. }
  434.  
  435. public void HandleJumping()
  436. {
  437. if (playerStats.currentStamina <= 0)
  438. {
  439. return;
  440. }
  441. if (playerManager.isInteracting)
  442. {
  443. return;
  444. }
  445. if(inputHandler.jump_Input)
  446. {
  447. if(inputHandler.moveAmount > 0)
  448. {
  449. moveDirection = cameraObject.forward * inputHandler.vertical;
  450. moveDirection += cameraObject.right * inputHandler.horizontal;
  451. animatorHandler.PlayTargetAnimation("JumpMove", true);
  452. moveDirection.y = 0;
  453. Quaternion jumpRotation = Quaternion.LookRotation(moveDirection);
  454. myTransform.rotation = jumpRotation;
  455. }
  456. else
  457. {
  458.  
  459. animatorHandler.PlayTargetAnimation("JumpIdle", true);
  460. moveDirection.y = 0;
  461.  
  462. }
  463. }
  464. }
  465. #endregion
  466.  
  467.  
  468. public void DodgeForward()
  469. {
  470. moveDirection = cameraObject.forward * inputHandler.vertical;
  471. moveDirection += cameraObject.right * inputHandler.horizontal;
  472. moveDirection.Normalize();
  473. moveDirection.y = 0;
  474. rigidbody.AddForce(moveDirection * 10000 * Time.deltaTime);
  475. }
  476.  
  477.  
  478. }
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement