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

public class CoinDrag : MonoBehaviour {

    public bool draggingCoin = false;
    public GameObject Coin;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit, 100f))
            {
                
                if (hit.transform.name == Coin.gameObject.name)
                {

                    draggingCoin = true;
                }
            }
            
        }
        if(Input.GetMouseButtonUp(0))
        {
            draggingCoin = false;
        }

        if(draggingCoin)
        {
            RaycastHit[] hits;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            hits = Physics.RaycastAll(ray, 100f);
        
            for(int i = 0; i < hits.Length; i++)
            {
                if(hits[i].transform.name == "Map")
                {
                    Coin.transform.position = hits[i].point;
                }
            }
            
        }
	}

    

}
