Touch & smell sensors

This commit is contained in:
Pascal Serrarens 2025-02-17 12:56:39 +01:00
parent d88d3a879d
commit 6551c653c3
2 changed files with 73 additions and 0 deletions

16
Sensors/TouchSensor.cs Normal file
View File

@ -0,0 +1,16 @@
namespace Passer.Control.Core {
public class TouchSensor : Thing {
public Thing touchedThing = null;
public TouchSensor(bool invokeEvent = true) : base(invokeEvent) {
}
#if UNITY_5_3_OR_NEWER
public override void CreateComponent() {
this.component = Unity.TouchSensor.Create(this);
this.component.core = this;
}
#endif
}
}

57
Unity/TouchSensor.cs Normal file
View File

@ -0,0 +1,57 @@
#if UNITY_5_3_OR_NEWER
using System.Collections;
using UnityEngine;
namespace Passer.Control.Unity {
public class TouchSensor : Thing {
public Core.TouchSensor coreSensor {
get => (Core.TouchSensor)base.core;
}
protected virtual void Start() {
if (core == null)
SetCoreThing(new Core.TouchSensor());
//StartCoroutine(MeasureDistance());
}
bool update = false;
protected override void Update() {
base.Update();
this.update= false;
}
public static TouchSensor Create(Core.TouchSensor core) {
GameObject gameObj = core.name != null ?
new(core.name) :
new("Touch Sensor");
TouchSensor component = gameObj.AddComponent<TouchSensor>();
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
rb.isKinematic = true;
SphereCollider collider = gameObj.AddComponent<SphereCollider>();
collider.radius = 0.01F;
collider.isTrigger = true;
component.core = core;
if (core.parent != null && core.parent.component != null)
gameObj.transform.SetParent(core.parent.component.transform, false);
if (core.position != null)
gameObj.transform.localPosition = core.position.ToVector3();
return component;
}
private void OnTriggerEnter(Collider other) {
Debug.Log("Touch!");
this.coreSensor.touchedThing = other.transform.GetComponentInParent<Thing>().core;
}
private void OnTriggerExit(Collider other) {
this.coreSensor.touchedThing = null;
}
}
}
#endif