using UnityEngine;

public class CarBodyShake : MonoBehaviour 
{

    public float Rate = 10f;
    public float Distance = 0.02f;
    public float LurchMax = 0.1f;

    private Rigidbody2D rb;
    private Vector2 lastVel;

    void Start()
    {
        rb = transform.root.GetComponent<Rigidbody2D>();
    }

	void FixedUpdate () 
	{
        Vector2 vshift = rb.velocity - lastVel;
        lastVel = rb.velocity;
        
        Vector3 lurchOffset = transform.InverseTransformDirection(LurchMax * -vshift.normalized);

        float yshake = Distance * Mathf.Sin(Time.time * Rate);
        transform.localPosition = new Vector3(0, yshake, 0) + lurchOffset;		
	}
}
