43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
#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)
|
|
SetCoreThing(new Core.DistanceSensor());
|
|
|
|
StartCoroutine(MeasureDistance());
|
|
}
|
|
|
|
public static DistanceSensor Create(Core.Thing parent) {
|
|
GameObject distanceObj = new("Distance sensor");
|
|
DistanceSensor component = distanceObj.AddComponent<DistanceSensor>();
|
|
if (parent != null && parent.component != null)
|
|
distanceObj.transform.SetParent(parent.component.transform);
|
|
|
|
return component;
|
|
}
|
|
|
|
IEnumerator MeasureDistance() {
|
|
while (Application.isPlaying) {
|
|
if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hitInfo, 10.0f)) {
|
|
core.distance = hitInfo.distance;
|
|
|
|
// send distance to...
|
|
yield return new WaitForSeconds(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
#endif |