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

[RequireComponent(typeof(Rigidbody))]
public class CenterOfMass : MonoBehaviour
{
    public Vector3 centerOfMass;
    public bool applyConstantly = true;

    Rigidbody rb;
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.centerOfMass = centerOfMass;
    }

    private void FixedUpdate()
    {
        if (applyConstantly)
            rb.centerOfMass = centerOfMass;
    }

    private void OnDrawGizmosSelected()
    {
        //draw center of mass
        if (GetComponent<Rigidbody>() != null)
        {
            Gizmos.color = Color.magenta;
            Gizmos.DrawWireSphere(GetComponent<Rigidbody>().worldCenterOfMass, 0.25f);
        }

        Gizmos.color = Color.white;
        Gizmos.DrawWireSphere(transform.TransformPoint(centerOfMass), 0.20f);
    }
}
