#if UNITY_5_3_OR_NEWER
using UnityEngine;
namespace RoboidControl.Unity {
///
/// The Unity representation of a Roboid Control touch sensor
///
public class TouchSensor : Thing {
///
/// The core touch sensor
///
public RoboidControl.TouchSensor coreSensor => base.core as RoboidControl.TouchSensor;
///
/// Create the Unity representation of the touch sensor
///
/// The core touch sensor
/// The Unity representation of the touch sensor
/// This uses a 'TouchSensor' resource when available for the Unity representation.
/// If this is not available, a default representation is created.
public static TouchSensor Create(RoboidControl.TouchSensor coreSensor) {
TouchSensor touchSensor;
GameObject prefab = (GameObject)Resources.Load("TouchSensor");
if (prefab != null) {
// Use resource prefab when available
GameObject gameObj = Instantiate(prefab);
touchSensor = gameObj.GetComponent();
touchSensor.Init(coreSensor);
}
else {
// Default implementation
GameObject gameObj = new(coreSensor.name);
touchSensor = gameObj.AddComponent();
touchSensor.Init(coreSensor);
Rigidbody rb = gameObj.AddComponent();
rb.isKinematic = true;
SphereCollider collider = gameObj.AddComponent();
collider.radius = 0.01f;
collider.isTrigger = true;
}
return touchSensor;
}
///
/// Handle touch trigger collider enter event
///
/// The collider which entered our trigger collider
private void OnTriggerEnter(Collider other) {
// Don't detect trigger colliders
if (other.isTrigger)
return;
// Don't touch yourself
if (this.transform.root == other.transform.root)
return;
this.coreSensor.touchedSomething = true;
this.core.updateQueue.Enqueue(new RoboidControl.Thing.CoreEvent(BinaryMsg.Id));
}
///
/// Handl touch trigger collider exit event
///
/// The collider which exited our trigger collider
private void OnTriggerExit(Collider other) {
if (other.isTrigger)
return;
this.coreSensor.touchedSomething = false;
}
}
}
#endif