using UnityEngine; namespace NanoBrain.Breitenberg { public class Sensor : MonoBehaviour { public float sensorRange = 10f; public LayerMask senseLayer; // layer for "light" objects public float output {get; protected set; } protected virtual void FixedUpdate() { output = SampleSensor(this.transform); } protected virtual float SampleSensor(Transform sensor) { // Cast a short set of rays in a cone and accumulate "brightness" from hits. int rays = 7; float halfAngle = 30f; float total = 0f; for (int i = 0; i < rays; i++) { float t = rays == 1 ? 0.5f : (float)i / (rays - 1); float angle = Mathf.Lerp(-halfAngle, halfAngle, t); Vector3 dir = Quaternion.AngleAxis(angle, sensor.up) * sensor.forward; Debug.DrawRay(sensor.position, dir * sensorRange); if (Physics.Raycast(sensor.position, dir, out RaycastHit hit, sensorRange, senseLayer)) { // Strength inversely proportional to distance, clamped to [0,1] float str = 1f - (hit.distance / sensorRange); // You can also sample material emission or color here if desired total += Mathf.Clamp01(str); } } return Mathf.Clamp01(total / rays); } } }