Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RBPuppetController : MonoBehaviour
  6. {
  7.  
  8. [SerializeField] private float RunSpeed = 4;
  9. [SerializeField] private float walkSpeed = 4;
  10. [SerializeField] private float gravity = -12;
  11. [SerializeField] private float jumpHeight = 1;
  12.  
  13. [SerializeField] private float turnSmoothTime = 0.2f;
  14. [SerializeField] private float turnSmoothVelocity;
  15. [SerializeField] private float speedSmoothTime = 0.1f;
  16. [SerializeField] private float speedSmoothVelocity;
  17.  
  18.  
  19. [SerializeField] private bool lockCursor;
  20.  
  21. float velocityY;
  22. float currentSpeed;
  23.  
  24. Transform cameraT;
  25. Animator animator;
  26. CharacterController controller;
  27.  
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. cameraT = Camera.main.transform;
  32. animator = GetComponent<Animator>();
  33. controller = GetComponent<CharacterController>();
  34.  
  35. if (lockCursor)
  36. {
  37. Cursor.lockState = CursorLockMode.Locked;
  38. Cursor.visible = false;
  39. }
  40. }
  41.  
  42. // Update is called once per frame
  43. void Update()
  44. {
  45. //inputAssignments --- Is it wiser to call buttons within Update to execute functions rather than within the functions themselves?
  46. Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  47. Vector2 inputDir = input.normalized;
  48. bool running = Input.GetButton("Sprint");
  49.  
  50. PlayerMovement(inputDir, running);
  51.  
  52. if (Input.GetButton("Jump"))
  53. {
  54.  
  55. Jump();
  56.  
  57.  
  58. }
  59. //Animation Control
  60. float animationSpeedPercent = ((running) ? currentSpeed / RunSpeed : currentSpeed / walkSpeed);
  61. animator.SetFloat("RunBlendSpeed", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
  62. }
  63.  
  64. void PlayerMovement(Vector2 inputDir, bool running)
  65. {
  66.  
  67. if (inputDir != Vector2.zero)
  68. {
  69. //Calculates the character rotation using the magic, of TRIG
  70. float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
  71. transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
  72.  
  73. }
  74.  
  75.  
  76. float targetSpeed = ((running) ? walkSpeed : RunSpeed) * inputDir.magnitude;
  77. currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
  78.  
  79. velocityY = Time.deltaTime * gravity;
  80. Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
  81.  
  82. controller.Move(velocity * Time.deltaTime);
  83. currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
  84.  
  85. if (controller.isGrounded)
  86. {
  87. velocityY = 0;
  88. }
  89.  
  90.  
  91. }
  92.  
  93.  
  94. void Jump()
  95. {
  96.  
  97. if (controller.isGrounded)
  98. {
  99. float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
  100. velocityY = jumpVelocity;
  101.  
  102. }
  103.  
  104.  
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement