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

public class ball_move : MonoBehaviour {
    public float speed = 1.0f;

    GameObject player;
    Rigidbody rigdbody;
    Vector3 forceDirect;

    void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        rigdbody = GetComponent<Rigidbody>();
    }

    void OnTriggerStay(Collider other)
    {
        if(other.gameObject == player)
        {
            forceDirect.x = player.transform.position.x > forceDirect.x ? -1f : 1f;
            forceDirect.y = 0;
            forceDirect.z = player.transform.position.z > forceDirect.z ? -1f : 1f;

            rigdbody.AddForce(forceDirect * speed, ForceMode.Impulse);
        }
    }

}