Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
TargetRigidbodyActuator.cs
Go to the documentation of this file.
1 using System;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Actuators {
12  [AddComponentMenu(ActuatorComponentMenuPath._ComponentMenuPath
13  + "TargetRigidbody"
14  + ActuatorComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(Rigidbody))]
18  string _movement;
19  AbstractPrototypingEnvironment _parent_environment;
20 
23  [SerializeField]
24  protected Rigidbody _Rigidbody;
25 
26  string _turn;
27 
31  public override string PrototypingTypeName { get { return "TargetRigidbody"; } }
32 
36  public Single MovementSpeed { get; set; }
37 
41  public Single RotationSpeed { get; set; }
42 
46  public void PreStep() { }
47 
51  public void Step() { this.OnStep(); }
52 
56  public void PostStep() { }
57 
61  protected override void Setup() {
62  this._Rigidbody = this.GetComponent<Rigidbody>();
63 
64  this._movement = this.Identifier + "Movement_";
65  this._turn = this.Identifier + "Turn_";
66  }
67 
71  protected override void RegisterComponent() {
72  this.Parent =
73  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._movement);
74  this.Parent =
75  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._turn);
76 
77  this._parent_environment =
78  NeodroidUtilities.RegisterComponent(this._parent_environment,
79  this);
80 
81  if (this._parent_environment != null) {
82  this._parent_environment.PreStepEvent += this.PreStep;
83  this._parent_environment.StepEvent += this.Step;
84  this._parent_environment.PostStepEvent += this.PostStep;
85  }
86  }
87 
92  protected override void InnerApplyMotion(IMotion motion) {
93  if (motion.ActuatorName == this._movement) {
94  this.ApplyMovement(motion.Strength);
95  } else if (motion.ActuatorName == this._turn) {
96  this.ApplyRotation(motion.Strength);
97  }
98  }
99 
100  void ApplyRotation(float rotation_change = 0f) { this.RotationSpeed = rotation_change; }
101 
102  void ApplyMovement(float movement_change = 0f) { this.MovementSpeed = movement_change; }
103 
104  void OnStep() {
105  this._Rigidbody.velocity = Vector3.zero;
106  this._Rigidbody.angularVelocity = Vector3.zero;
107 
108  // Move
109  var movement = this.MovementSpeed * Time.deltaTime * this.transform.forward;
110  this._Rigidbody.MovePosition(this._Rigidbody.position + movement);
111 
112  // Turn
113  var turn = this.RotationSpeed * Time.deltaTime;
114  var turn_rotation = Quaternion.Euler(0f, turn, 0f);
115  this._Rigidbody.MoveRotation(this._Rigidbody.rotation * turn_rotation);
116  }
117  }
118 }