#if UNITY_5_3_OR_NEWER using UnityEngine; namespace RoboidControl.Unity { /// /// The Unity representation of the TouchSensor /// public class TouchSensor : Thing { // public SiteServer participant; /// /// The core touch sensor /// public RoboidControl.TouchSensor coreSensor { get => (RoboidControl.TouchSensor)base.core; } /// /// Start the Unity represention /// protected virtual void Start() { if (core == null) { participant = FindAnyObjectByType(); SetCoreThing(new RoboidControl.TouchSensor(participant.site)); } } /// /// Create the Unity representation /// /// The core touch sensor /// The Unity representation of the touch sensor public static TouchSensor Create(RoboidControl.TouchSensor core) { GameObject gameObj = core.name != null ? new(core.name) : new("Touch Sensor"); TouchSensor component = gameObj.AddComponent(); component.Init(core); Rigidbody rb = gameObj.AddComponent(); rb.isKinematic = true; SphereCollider collider = gameObj.AddComponent(); collider.radius = 0.01f; collider.isTrigger = true; if (gameObj.transform.parent != null && gameObj.transform.localPosition.magnitude > 0) { collider.radius = Vector3.Distance(gameObj.transform.position, gameObj.transform.parent.position) / 2; gameObj.transform.position = (gameObj.transform.position + gameObj.transform.parent.position) / 2; } return component; } private void OnTriggerEnter(Collider other) { if (other.isTrigger) return; if (this.transform.root == other.transform.root) return; Debug.Log($"*** {this} Touch"); this.coreSensor.touchedSomething = true; } private void OnTriggerExit(Collider other) { if (other.isTrigger) return; Debug.Log($"*** {this} Touch end"); this.coreSensor.touchedSomething = false; } } } #endif