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

public class picking : MonoBehaviour
{
    Camera cam;
    public int counter=0;

    void Start()
    {
        cam = GetComponent<Camera>();
    }


    void Update()
    {
        //RayCast();
        if (Input.GetButtonDown("Fire1"))
        {
            RayCast();   
        }
    }

    // Update is called once per frame
    void RayCast()
    {
        // Bit shift the index of the layer (8) to get a bit mask
        int layerMask = 1 << 8;

        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        layerMask = ~layerMask;

        Ray ray = cam.ScreenPointToRay(Input.mousePosition); // Create ray from camera into the world through the mouse cursor position.
        RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity, layerMask))
        {
             if (hit.collider != null)
            {
                if( hit.collider.tag == "Tree")
                {
                    counter++;
                    Debug.Log("hits : "+ counter);
                }
            }
        }
        else
        {
            //Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
            //Debug.Log("Did not Hit");
        }
    }
}
