Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RaycastSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Sensors.Rays {
7  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
8  + "Raycast"
9  + SensorComponentMenuPath._Postfix)]
10  public class RaycastSensor : Sensor,
11  IHasSingle {
12  [SerializeField] Vector3 _direction = Vector3.forward;
13 
14  [SerializeField] RaycastHit _hit = new RaycastHit();
15 
16  [SerializeField]
17  Space1 _observation_space = new Space1 {_Decimal_Granularity = 3, _Min_Value = 0, _Max_Value = 100.0f};
18 
19  [Header("Observation", order = 103)]
20  [SerializeField]
21  float _observation_value = 0;
22 
23  [SerializeField] Space1 _observation_value_space = Space1.ZeroOne;
24 
25  [SerializeField] float _range = 100.0f;
26 
27  public override string PrototypingTypeName {
28  get { return "Raycast" + $"{this._direction.x}{this._direction.y}{this._direction.z}"; }
29  }
30 
31  public Space1 SingleSpace { get { return this._observation_value_space; } }
32 
33  public float ObservationValue {
34  get { return this._observation_value; }
35  private set {
36  this._observation_value = this.SingleSpace.IsNormalised
37  ? this._observation_space.ClipNormaliseRound(value)
38  : value;
39  }
40  }
41 
42  protected override void PreSetup() { }
43 
44  public override IEnumerable<float> FloatEnumerable { get { return new[] {this.ObservationValue}; } }
45 
46  public override void UpdateObservation() {
47  if (Physics.Raycast(this.transform.position, this._direction, out this._hit, this._range)) {
48  this.ObservationValue = this._hit.distance;
49  } else {
50  this.ObservationValue = this._range;
51  }
52  }
53 
54  #if UNITY_EDITOR
55  [SerializeField] Color _color = Color.green;
56 
57  void OnDrawGizmosSelected() {
58  if (this.enabled) {
59  var position = this.transform.position;
60  Debug.DrawLine(position, position - this._direction * this._range, this._color);
61  }
62  }
63  #endif
64  }
65 }