#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;
set => base.core = value;
}
SphereCollider touchCollider = null;
///
/// Start the Unity represention
///
protected virtual void Start() {
if (core == null) {
participant = FindAnyObjectByType();
SetCoreThing(new RoboidControl.TouchSensor(participant.site));
}
touchCollider = GetComponent();
}
///
/// Create the Unity representation
///
/// The core touch sensor
/// The Unity representation of the touch sensor
public static TouchSensor Create(RoboidControl.TouchSensor core) {
GameObject prefab = (GameObject)Resources.Load("TouchSensor");
if (prefab != null) {
// Use resource prefab when available
GameObject gameObj = Instantiate(prefab);
TouchSensor component = gameObj.GetComponent();
if (component != null)
component.core = core;
return component;
}
else {
// Fallback implementation
GameObject gameObj = new(core.name);
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;
return component;
}
}
private void OnTriggerEnter(Collider other) {
// Debug.Log("Touch?");
if (other.isTrigger) {
// Debug.Log($" was trigger {other.name}");
return;
}
if (this.transform.root == other.transform.root) {
Debug.Log($" was myself {other.name}");
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