93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
#if UNITY_5_3_OR_NEWER
|
|
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
|
|
/// <summary>
|
|
/// The Unity representation of the TouchSensor
|
|
/// </summary>
|
|
public class TouchSensor : Thing {
|
|
|
|
// public SiteServer participant;
|
|
/// <summary>
|
|
/// The core touch sensor
|
|
/// </summary>
|
|
public RoboidControl.TouchSensor coreSensor {
|
|
get => (RoboidControl.TouchSensor)base.core;
|
|
set => base.core = value;
|
|
}
|
|
|
|
SphereCollider touchCollider = null;
|
|
|
|
/// <summary>
|
|
/// Start the Unity represention
|
|
/// </summary>
|
|
protected virtual void Start() {
|
|
if (core == null) {
|
|
participant = FindAnyObjectByType<SiteServer>();
|
|
SetCoreThing(new RoboidControl.TouchSensor(participant.site));
|
|
}
|
|
touchCollider = GetComponent<SphereCollider>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create the Unity representation
|
|
/// </summary>
|
|
/// <param name="core">The core touch sensor</param>
|
|
/// <returns>The Unity representation of the touch sensor</returns>
|
|
public static TouchSensor Create(RoboidControl.TouchSensor core) {
|
|
GameObject gameObj = core.name != null ?
|
|
new(core.name) :
|
|
new("Touch Sensor");
|
|
TouchSensor component = gameObj.AddComponent<TouchSensor>();
|
|
component.Init(core);
|
|
|
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
|
rb.isKinematic = true;
|
|
|
|
SphereCollider collider = gameObj.AddComponent<SphereCollider>();
|
|
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;
|
|
}
|
|
|
|
protected override void Update() {
|
|
base.Update();
|
|
if (touchCollider.radius == 0.01f &&
|
|
this.transform.parent != null && this.transform.localPosition.magnitude > 0
|
|
) {
|
|
touchCollider.radius = Vector3.Distance(this.transform.position, this.transform.parent.position) / 2;
|
|
this.transform.position = (this.transform.position + this.transform.parent.position) / 2;
|
|
}
|
|
}
|
|
|
|
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 |