using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDrag : MonoBehaviour {

	Transform thisTF;
	Rigidbody thisRB;

	[SerializeField] float headingDrag;
	[SerializeField] float headingCorrection;

	void Start () {
		thisTF = GetComponent<Transform> ();
		thisRB = GetComponent<Rigidbody> ();
	}
	

	void Update () {
		Vector3 heading = thisTF.forward;

		float headingDifference = Vector3.Distance (heading, thisRB.velocity.normalized);
		float drag = (headingDifference / 2.5f); //2.5 is the magic slowdown number

		thisRB.velocity = thisRB.velocity * (1 - (drag * headingDrag * Time.deltaTime));
	
		float speed = thisRB.velocity.magnitude;
		Vector3 newDirection = Vector3.Slerp (thisRB.velocity.normalized, heading, headingCorrection * Time.deltaTime).normalized;

		thisRB.velocity = newDirection * speed;
	}
}
