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

public class AddForceWithTrigger : MonoBehaviour
{
    public float forceAmount = 1f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnTriggerStay(Collider other)
    {
        // if the object in the collider has a rigid body, add force
        if(other.TryGetComponent<Rigidbody>(out Rigidbody rb))
        {
            // calculate the force based on distance from the center of this game object
            var dir = (other.transform.position - transform.position).normalized;
            other.GetComponent<Rigidbody>().AddForce(dir * forceAmount);
        }
    }
}
