Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour {
- [Header("Variables")]
- public Vector3 playerVelocity;
- public bool transition = false;
- private Rigidbody rb;
- private Coroutine horizontalMovementCoroutine;
- [Space]
- [Header("Acceleration")]
- //public Vector3 accelerationPerSecond = Vector3.one / 10f;
- public Vector3 maxVelocity;
- public float accelerationDuration;
- [Space]
- [Header("Deceleration")]
- //public Vector3 decelerationPerSecond = Vector3.one / 10f;
- public float decelerationDuration;
- private PlayerController playerController;
- // Use this for initialization
- void Start () {
- rb = GetComponent<Rigidbody>();
- playerController = GetComponent<PlayerController>();
- }
- // Update is called once per frame
- void Update () {
- //Check for Input
- if (Input.GetAxis("Horizontal") != 0)
- {
- if (horizontalMovementCoroutine == null) { horizontalMovementCoroutine = StartCoroutine(Accelerate()); }
- }
- else if (playerController.IsWalking())
- {
- if (horizontalMovementCoroutine == null) { horizontalMovementCoroutine = StartCoroutine(Decelerate()); }
- }
- }
- void FixedUpdate()
- {
- rb.velocity = playerVelocity;
- }
- IEnumerator Accelerate()
- {
- float timer = 0;
- Vector3 startVelocity = rb.velocity;
- while (timer < accelerationDuration)
- {
- timer = Mathf.Clamp(timer + Time.deltaTime, 0, accelerationDuration);
- float percentComplete = timer / accelerationDuration;
- playerVelocity = maxVelocity * Mathf.Pow(percentComplete, 2) + startVelocity;
- Debug.LogWarning("called");
- yield return null;
- }
- horizontalMovementCoroutine = null;
- }
- IEnumerator Decelerate()
- {
- Debug.Log("called");
- float timer = 0;
- Vector3 startVelocity = rb.velocity;
- while (timer < decelerationDuration)
- {
- timer = Mathf.Clamp(timer + Time.deltaTime, 0, decelerationDuration);
- float percentComplete = timer / decelerationDuration;
- playerVelocity = -maxVelocity * percentComplete * (percentComplete - 2) + startVelocity;
- //Debug.Log(-decelerationPerSecond * percentComplete * (percentComplete - 2) + startVelocity);
- yield return null;
- }
- playerVelocity = Vector3.zero;
- horizontalMovementCoroutine = null;
- //yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement