Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7. [Header("Variables")]
  8. public Vector3 playerVelocity;
  9. public bool transition = false;
  10. private Rigidbody rb;
  11. private Coroutine horizontalMovementCoroutine;
  12.  
  13. [Space]
  14.  
  15. [Header("Acceleration")]
  16. //public Vector3 accelerationPerSecond = Vector3.one / 10f;
  17. public Vector3 maxVelocity;
  18. public float accelerationDuration;
  19.  
  20. [Space]
  21.  
  22. [Header("Deceleration")]
  23. //public Vector3 decelerationPerSecond = Vector3.one / 10f;
  24. public float decelerationDuration;
  25.  
  26. private PlayerController playerController;
  27.  
  28. // Use this for initialization
  29. void Start () {
  30. rb = GetComponent<Rigidbody>();
  31. playerController = GetComponent<PlayerController>();
  32. }
  33.  
  34. // Update is called once per frame
  35. void Update () {
  36.  
  37. //Check for Input
  38. if (Input.GetAxis("Horizontal") != 0)
  39. {
  40. if (horizontalMovementCoroutine == null) { horizontalMovementCoroutine = StartCoroutine(Accelerate()); }
  41. }
  42. else if (playerController.IsWalking())
  43. {
  44. if (horizontalMovementCoroutine == null) { horizontalMovementCoroutine = StartCoroutine(Decelerate()); }
  45. }
  46. }
  47.  
  48. void FixedUpdate()
  49. {
  50. rb.velocity = playerVelocity;
  51. }
  52.  
  53. IEnumerator Accelerate()
  54. {
  55. float timer = 0;
  56. Vector3 startVelocity = rb.velocity;
  57. while (timer < accelerationDuration)
  58. {
  59. timer = Mathf.Clamp(timer + Time.deltaTime, 0, accelerationDuration);
  60. float percentComplete = timer / accelerationDuration;
  61. playerVelocity = maxVelocity * Mathf.Pow(percentComplete, 2) + startVelocity;
  62. Debug.LogWarning("called");
  63. yield return null;
  64. }
  65. horizontalMovementCoroutine = null;
  66. }
  67.  
  68. IEnumerator Decelerate()
  69. {
  70. Debug.Log("called");
  71. float timer = 0;
  72. Vector3 startVelocity = rb.velocity;
  73. while (timer < decelerationDuration)
  74. {
  75. timer = Mathf.Clamp(timer + Time.deltaTime, 0, decelerationDuration);
  76. float percentComplete = timer / decelerationDuration;
  77. playerVelocity = -maxVelocity * percentComplete * (percentComplete - 2) + startVelocity;
  78. //Debug.Log(-decelerationPerSecond * percentComplete * (percentComplete - 2) + startVelocity);
  79. yield return null;
  80. }
  81. playerVelocity = Vector3.zero;
  82. horizontalMovementCoroutine = null;
  83. //yield return null;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement