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

public class AccelerateObject : MonoBehaviour {

    public float effectRadius = 5f;

    public float strength = 1000f;

    public string[] tags;

	// Use this for initialization
	void Start () {
		
	}

    void Update()
    {
        if (Input.GetButtonDown("Jump"))
        { 
            strength *= -1;
        }
    }
	
	// Update is called once per frame
	void FixedUpdate () {
        Collider[] colliders = Physics.OverlapSphere(transform.position, effectRadius);
        
        for (int i =0; i<colliders.Length; i++){
            GameObject other = colliders[i].gameObject;
            Rigidbody body = other.GetComponent<Rigidbody>();
            bool tagged = false;
            for (int j = 0; j < tags.Length; j++){
                if (other.CompareTag(tags[j]))
                    tagged = true;
            }

            if (body != null && tagged){

                Vector3 direction = body.transform.position - transform.position;
                float magnitude = direction.magnitude;
                Vector3 nomalized = direction.normalized;
                float force = (effectRadius - magnitude)/effectRadius * strength;
                body.AddForce(nomalized * force);
            }
        }
	}

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(transform.position, effectRadius);
    }
}
