質問編集履歴
1
エラー文にある問題のスクリプトの中身を追記しました。両方は入らなかったので、片方だけですが念の為。
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
コメントいただけたらすぐにでも返信いたしますので、どうぞよろしくお願いいたします。
|
8
8
|
|
9
|
+
問題のスクリプトの内容も追記しておきますね。
|
10
|
+
|
9
11
|
### 発生している問題・エラーメッセージ
|
10
12
|
|
11
13
|
Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockWithInputPlayerController.cs(1,33): error CS0234: The type or namespace name 'Input' does not exist in the namespace 'Unity.MLAgents.Extensions' (are you missing an assembly reference?)
|
@@ -28,6 +30,242 @@
|
|
28
30
|
|
29
31
|
|
30
32
|
|
33
|
+
###スクリプトの内容
|
34
|
+
|
35
|
+
(PushBlockWithInputPlayerController)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
using Unity.MLAgents.Extensions.Input;
|
40
|
+
|
41
|
+
using UnityEngine;
|
42
|
+
|
43
|
+
using UnityEngine.InputSystem;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
/// <summary>
|
48
|
+
|
49
|
+
/// This class handles the input for the PushBlock Cube character in the PushBlock scene.
|
50
|
+
|
51
|
+
/// Note that the only ML-Agents code here is the implementation of the <see cref="IInputActionAssetProvider"/>.
|
52
|
+
|
53
|
+
/// The <see cref="InputActuatorComponent"/> looks for a component that implements that interface in order to
|
54
|
+
|
55
|
+
/// rebind actions to virtual controllers when training agents or running inference. This means that you can
|
56
|
+
|
57
|
+
/// keep your input handling code separate from ML-Agents, and have your agent's action space defined by the
|
58
|
+
|
59
|
+
/// actions defined in your project's <see cref="GetInputActionAsset"/>.
|
60
|
+
|
61
|
+
///
|
62
|
+
|
63
|
+
/// If you don't implement <see cref="IInputActionAssetProvider"/> the <see cref="InputActuatorComponent"/> will
|
64
|
+
|
65
|
+
/// look for a <see cref="PlayerInput"/> component on the GameObject it live on. It will rebind the actions of that
|
66
|
+
|
67
|
+
/// instance of the asset.
|
68
|
+
|
69
|
+
///
|
70
|
+
|
71
|
+
/// It is important to note that if you have multiple components on the same GameObject handling input, you will
|
72
|
+
|
73
|
+
/// need to share the instance of the generated C# <see cref="IInputActionCollection2"/> (named <see cref="m_PushBlockActions"/>
|
74
|
+
|
75
|
+
/// here) in order to ensure that all of your actions are bound correctly for ml-agents training and inference.
|
76
|
+
|
77
|
+
/// </summary>
|
78
|
+
|
79
|
+
public class PushBlockWithInputPlayerController : MonoBehaviour, IInputActionAssetProvider
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
PushBlockWithInputSettings m_PushBlockSettings;
|
86
|
+
|
87
|
+
public float JumpTime = 0.5f;
|
88
|
+
|
89
|
+
float m_JumpTimeRemaining;
|
90
|
+
|
91
|
+
Rigidbody m_PlayerRb; //cached on initialization
|
92
|
+
|
93
|
+
PushBlockActions m_PushBlockActions;
|
94
|
+
|
95
|
+
float m_JumpCoolDownStart;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
void Awake()
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
m_PushBlockSettings = FindObjectOfType<PushBlockWithInputSettings>();
|
104
|
+
|
105
|
+
LazyInitializeActions();
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// Cache the agent rigidbody
|
110
|
+
|
111
|
+
m_PlayerRb = GetComponent<Rigidbody>();
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
void LazyInitializeActions()
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
if (m_PushBlockActions != null)
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
return;
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
m_PushBlockActions = new PushBlockActions();
|
132
|
+
|
133
|
+
m_PushBlockActions.Enable();
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// You can listen to C# events.
|
138
|
+
|
139
|
+
m_PushBlockActions.Movement.jump.performed += JumpOnperformed;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
void JumpOnperformed(InputAction.CallbackContext callbackContext)
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
InnerJump(gameObject.transform);
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
void FixedUpdate()
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
// Or you can poll the action itself like we do here.
|
160
|
+
|
161
|
+
InnerMove(gameObject.transform, m_PushBlockActions.Movement.movement.ReadValue<Vector2>());
|
162
|
+
|
163
|
+
if (m_JumpTimeRemaining < 0)
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
m_PlayerRb.AddForce(-transform.up * (m_PushBlockSettings.agentJumpForce * 3), ForceMode.Acceleration);
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
m_JumpTimeRemaining -= Time.fixedDeltaTime;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
void InnerJump(Transform t)
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
if (Time.realtimeSinceStartup - m_JumpCoolDownStart > m_PushBlockSettings.agentJumpCoolDown)
|
184
|
+
|
185
|
+
{
|
186
|
+
|
187
|
+
m_JumpTimeRemaining = JumpTime;
|
188
|
+
|
189
|
+
m_PlayerRb.AddForce(t.up * m_PushBlockSettings.agentJumpForce, ForceMode.VelocityChange);
|
190
|
+
|
191
|
+
m_JumpCoolDownStart = Time.realtimeSinceStartup;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
void InnerMove(Transform t, Vector2 v)
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
var forward = CreateForwardVector(v);
|
204
|
+
|
205
|
+
var up = CreateUpVector(v);
|
206
|
+
|
207
|
+
var dirToGo = t.forward * forward;
|
208
|
+
|
209
|
+
var rotateDir = t.up * up;
|
210
|
+
|
211
|
+
t.Rotate(rotateDir, Time.deltaTime * 200f);
|
212
|
+
|
213
|
+
m_PlayerRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed,
|
214
|
+
|
215
|
+
ForceMode.VelocityChange);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
static float CreateUpVector(Vector2 move)
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
return Mathf.Abs(move.x) > Mathf.Abs(move.y) ? move.x : 0f;
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
static float CreateForwardVector(Vector2 move)
|
232
|
+
|
233
|
+
{
|
234
|
+
|
235
|
+
return Mathf.Abs(move.y) > Mathf.Abs(move.x) ? move.y : 0f;
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
/// <summary>
|
242
|
+
|
243
|
+
/// This is the implementation of the <see cref="IInputActionAssetProvider"/> for this class. We need
|
244
|
+
|
245
|
+
/// both the <see cref="GetInputActionAsset"/> and the <see cref="IInputActionCollection2"/> if you are
|
246
|
+
|
247
|
+
/// listening to C# events, Unity Events, or receiving Messages from the Input System Package as those callbacks
|
248
|
+
|
249
|
+
/// are set up through the generated <see cref="IInputActionCollection2"/>.
|
250
|
+
|
251
|
+
/// </summary>
|
252
|
+
|
253
|
+
/// <returns></returns>
|
254
|
+
|
255
|
+
public (InputActionAsset, IInputActionCollection2) GetInputActionAsset()
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
LazyInitializeActions();
|
260
|
+
|
261
|
+
return (m_PushBlockActions.asset, m_PushBlockActions);
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
31
269
|
### 試したこと
|
32
270
|
|
33
271
|
|