Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
LineOfSightSensor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Sensors.Rays {
11  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
12  + "LineOfSight"
13  + SensorComponentMenuPath._Postfix)]
14  [ExecuteInEditMode]
15  [Serializable]
16  public class LineOfSightSensor : Sensor,
17  IHasSingle {
18  RaycastHit _hit = new RaycastHit();
19 
20  [SerializeField] float _obs_value = 0;
21 
24  [SerializeField]
25  Space1 _observation_value_space = Space1.ZeroOne;
26 
27  [Header("Specific", order = 102)]
28  [SerializeField]
29  UnityEngine.Transform _target = null;
30 
31  public override string PrototypingTypeName { get { return "LineOfSight"; } }
32 
33  public float ObservationValue { get { return this._obs_value; } private set { this._obs_value = value; } }
34 
35  public Space1 SingleSpace { get { return this._observation_value_space; } }
36 
37  protected override void PreSetup() { }
38 
39  public override IEnumerable<float> FloatEnumerable { get { return new[] {this.ObservationValue}; } }
40 
41  public override void UpdateObservation() {
42  var distance = Vector3.Distance(this.transform.position, this._target.position);
43  if (Physics.Raycast(this.transform.position,
44  this._target.position - this.transform.position,
45  out this._hit,
46  distance)) {
47  #if NEODROID_DEBUG
48  if (this.Debugging) {
49  Debug.Log(this._hit.distance);
50  }
51  #endif
52 
53  if (this._hit.collider.gameObject != this._target.gameObject) {
54  this.ObservationValue = 0;
55  } else {
56  this.ObservationValue = 1;
57  }
58  } else {
59  this.ObservationValue = 1;
60  }
61  }
62  }
63 }