using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;

//using UnityEditor;
using UnityEngine;
//using UnityEngine.UI;
//using Uhity.Entities;

using URD = UnityEngine.Random;

public class ClampCheckCast : MonoBehaviour {
	
	public LayerMask mask = 1<<8 | 1;
	
	public float radios = 0.25f;
	public float distance = 1;
	public float checkRange = 0.5f;
	public GameObject star;
	public bool isHit = false;
	
	private void CheckHit() {
		RaycastHit hit;
		
		if(Physics.SphereCast(transform.position, radios, -Vector3.up, out hit, Mathf.Infinity, mask)) {
			
			var hitDis = hit.distance;
			
			isHit = Mathf.Abs(hitDis - distance) < checkRange;
			
		}
		
		star.SetActive(isHit);
	}
	
	
	// Update is called once per frame
	void Update() {
		CheckHit();
	}
	
	void OnDrawGizmos() {
		Gizmos.matrix = transform.localToWorldMatrix;
		
		Gizmos.color = isHit ? Color.green : Color.red;
		Gizmos.DrawWireCube(Vector3.up * -distance/2f, new Vector3(radios, -distance, radios));
		Gizmos.DrawWireCube(Vector3.up * -distance/2f -Vector3.up * checkRange, new Vector3(radios, -distance, radios));
		
	}
}
