using UnityEngine;
using System.Collections;

public class AttackScript : MonoBehaviour {

	public GameObject raycastObject;
	public float treeHitDistance = 1.25f;

	void Start () {

	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if(Input.GetButtonDown("AxeHit") || Cardboard.SDK.CardboardTriggered) {
		//if(Cardboard.SDK.CardboardTriggered) {
			if(CheckForHit()) {
				Debug.Log ("Tree hit");
			}
		}
	}


	bool CheckForHit() {
		RaycastHit objectHit;
		Vector3 fwd = raycastObject.transform.TransformDirection(Vector3.forward);
		Debug.DrawRay(raycastObject.transform.position, fwd*treeHitDistance, Color.red);

		if(Physics.Raycast(raycastObject.transform.position, fwd, out objectHit, treeHitDistance))
		{
			//you've hit the object!
			//NOW DO SOMETHING

			GameObject treeHit = objectHit.transform.gameObject;
			TreeHealth treeScript = (TreeHealth) treeHit.GetComponent<TreeHealth>();
			treeScript.hitTree();

			return true;
		}
		else
		{
			Debug.Log("Missed any tree");
			return false;

		}

	}
}


