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

public class MagnetScript : MonoBehaviour {

    private bool isActive = false;
    public float power = 10.0f;

    private Rigidbody playerRb;

    private void Start()
    {
        playerRb = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody>();
    }


    void FixedUpdate () {
        if (isActive)
        {
            playerRb.AddForce((playerRb.transform.position - transform.position).normalized * Time.fixedDeltaTime * power); 
        }
	}


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isActive = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isActive = false;
        }
    }
}
