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

public class CenterOfMass2D : MonoBehaviour
{
    public Vector2 offset;
    public bool applyOnUpdate;

    private void Start()
    {
        Apply();
    }

    private void FixedUpdate()
    {
        if (applyOnUpdate)
            Apply();
    }

    private void Apply()
    {
        GetComponent<Rigidbody2D>().centerOfMass = offset;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.cyan;
        Gizmos.DrawWireSphere(transform.TransformPoint(offset), 0.05f);

        Gizmos.color = Color.magenta;
        Gizmos.DrawWireSphere(GetComponent<Rigidbody2D>().worldCenterOfMass, 0.1f);
    }
}
