Guest User

Untitled

a guest
Sep 13th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class cheatMovement : MonoBehaviour
  6. {
  7.  
  8. public float walkSpeed = 2;
  9. public float runSpeed = 6;
  10. public float gravity = -12;
  11. public float jumpHeight = 1;
  12. [Range(0, 1)]
  13. public float airControlPercent;
  14.  
  15. public float turnSmoothTime = 0.2f;
  16. float turnSmoothVelocity;
  17.  
  18. public float speedSmoothTime = 0.1f;
  19. float speedSmoothVelocity;
  20. float currentSpeed;
  21. float velocityY;
  22.  
  23. Animator animator;
  24. Transform cameraT;
  25. CharacterController controller;
  26.  
  27. void Start()
  28. {
  29. animator = GetComponent<Animator>();
  30. cameraT = Camera.main.transform;
  31. controller = GetComponent<CharacterController>();
  32. }
  33.  
  34. void Update()
  35. {
  36. // input
  37. Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  38. Vector2 inputDir = input.normalized;
  39. bool running = Input.GetKey(KeyCode.LeftShift);
  40.  
  41. Move(inputDir, running);
  42.  
  43. if (Input.GetKeyDown(KeyCode.Space))
  44. {
  45. Jump();
  46. }
  47. // animator
  48. float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
  49. animator.SetFloat("RunBlendSpeed", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
  50.  
  51. }
  52.  
  53. void Move(Vector2 inputDir, bool running)
  54. {
  55. if (inputDir != Vector2.zero)
  56. {
  57. float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
  58. transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
  59. }
  60.  
  61. float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
  62. currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
  63.  
  64. velocityY += Time.deltaTime * gravity;
  65. Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
  66.  
  67. controller.Move(velocity * Time.deltaTime);
  68. currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
  69.  
  70. if (controller.isGrounded)
  71. {
  72. velocityY = 0;
  73. }
  74.  
  75. }
  76.  
  77. void Jump()
  78. {
  79. if (controller.isGrounded)
  80. {
  81. float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
  82. velocityY = jumpVelocity;
  83. }
  84. }
  85.  
  86. float GetModifiedSmoothTime(float smoothTime)
  87. {
  88. if (controller.isGrounded)
  89. {
  90. return smoothTime;
  91. }
  92.  
  93. if (airControlPercent == 0)
  94. {
  95. return float.MaxValue;
  96. }
  97. return smoothTime / airControlPercent;
  98. }
  99. }
Add Comment
Please, Sign In to add comment