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

public class HitObjectController : MonoBehaviour {
	private const float MaxDistance = 20f;

	[SerializeField]private Collider[] colliders = null;

	void Start () {
		FindColliders ();
	}

	[ContextMenu("Find control colliders")]
	void FindControlColliders () {
		colliders = null;
		FindColliders ();
	}

	void FindColliders () {
		if (colliders == null || colliders.Length == 0) {
			colliders = GetComponentsInChildren<Collider> ();
		}
	}

	public bool IsHit(Ray hitRay) {
		if (colliders == null || colliders.Length == 0) {
			Debug.LogError ("No colliders in hit object " + name);
			return false;
		}
		foreach (var collider in colliders) {
			RaycastHit hit;
			if (collider.Raycast(hitRay, out hit, MaxDistance)) {
				return true;
			}
		}
		return false;
	}

#if UNITY_EDITOR
	void OnDrawGizmos () {
		FindColliders ();
	}
#endif
}
