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

public class TunnelGravityField : MonoBehaviour
{
    public float strength = -9.81f;
    public bool drawGravityRays = true;

    private void OnTriggerStay(Collider other)
    {
        if (other.attachedRigidbody != null && other.attachedRigidbody.isKinematic == false)
        {
            Vector3 localPos = transform.InverseTransformPoint(other.attachedRigidbody.position);
            localPos.z = 0;
            localPos.Normalize();
            localPos *= -1;

            Vector3 gravityVector = transform.TransformDirection(localPos) * strength;
            other.attachedRigidbody.AddForce(gravityVector, ForceMode.Acceleration);

            if(drawGravityRays)
                Debug.DrawRay(other.attachedRigidbody.position, gravityVector);
        }
    }
}
