#if UNITY_5_3_OR_NEWER using System.Collections; using UnityEngine; namespace Passer.Control.Unity { public class DistanceSensor : Thing { public new Core.DistanceSensor core { get => (Core.DistanceSensor)base.core; set => base.core = value; } protected virtual void Start() { if (core == null) { SiteServer siteServer = FindAnyObjectByType(); SetCoreThing(new Core.DistanceSensor(siteServer.site)); } StartCoroutine(MeasureDistance()); } public static DistanceSensor Create(Core.Thing parent) { GameObject distanceObj = new("Distance sensor"); DistanceSensor component = distanceObj.AddComponent(); if (parent != null && parent.component != null) distanceObj.transform.SetParent(parent.component.transform, false); return component; } IEnumerator MeasureDistance() { while (Application.isPlaying) { if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hitInfo, 2.0f)) { Thing thing = hitInfo.transform.GetComponentInParent(); if (thing == null) { // Debug.Log($"collision {hitInfo.transform.name} {hitInfo.distance}"); core.distance = hitInfo.distance; } else core.distance = 0; } yield return new WaitForSeconds(0.1f); } } } } #endif