teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

エラー文にある問題のスクリプトの中身を追記しました。両方は入らなかったので、片方だけですが念の為。

2021/09/03 17:49

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,7 @@
2
2
  こんにちは。1-2か月前pythonのプログラミングを始めた超初心者です。UnityのML-Agentsのサンプル集を見て強化学習に興味を持ったので、とりあえずサンプルを使って学習をやってみよう!と思ったのですが、うまくいっておりません。
3
3
  最初の方は30個くらいエラーがあったのですが、パッケージマネージャーからInput systemなるものを導入することで5個まで減りました。
4
4
  コメントいただけたらすぐにでも返信いたしますので、どうぞよろしくお願いいたします。
5
+ 問題のスクリプトの内容も追記しておきますね。
5
6
  ### 発生している問題・エラーメッセージ
6
7
  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?)
7
8
 
@@ -13,6 +14,124 @@
13
14
 
14
15
  Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockActions.cs(237,17): error CS0540: 'PushBlockActions.IEnumerable.GetEnumerator()': containing type does not implement interface 'IEnumerable'
15
16
 
17
+ ###スクリプトの内容
18
+ (PushBlockWithInputPlayerController)
19
+
20
+ using Unity.MLAgents.Extensions.Input;
21
+ using UnityEngine;
22
+ using UnityEngine.InputSystem;
23
+
24
+ /// <summary>
25
+ /// This class handles the input for the PushBlock Cube character in the PushBlock scene.
26
+ /// Note that the only ML-Agents code here is the implementation of the <see cref="IInputActionAssetProvider"/>.
27
+ /// The <see cref="InputActuatorComponent"/> looks for a component that implements that interface in order to
28
+ /// rebind actions to virtual controllers when training agents or running inference. This means that you can
29
+ /// keep your input handling code separate from ML-Agents, and have your agent's action space defined by the
30
+ /// actions defined in your project's <see cref="GetInputActionAsset"/>.
31
+ ///
32
+ /// If you don't implement <see cref="IInputActionAssetProvider"/> the <see cref="InputActuatorComponent"/> will
33
+ /// look for a <see cref="PlayerInput"/> component on the GameObject it live on. It will rebind the actions of that
34
+ /// instance of the asset.
35
+ ///
36
+ /// It is important to note that if you have multiple components on the same GameObject handling input, you will
37
+ /// need to share the instance of the generated C# <see cref="IInputActionCollection2"/> (named <see cref="m_PushBlockActions"/>
38
+ /// here) in order to ensure that all of your actions are bound correctly for ml-agents training and inference.
39
+ /// </summary>
40
+ public class PushBlockWithInputPlayerController : MonoBehaviour, IInputActionAssetProvider
41
+ {
42
+
43
+ PushBlockWithInputSettings m_PushBlockSettings;
44
+ public float JumpTime = 0.5f;
45
+ float m_JumpTimeRemaining;
46
+ Rigidbody m_PlayerRb; //cached on initialization
47
+ PushBlockActions m_PushBlockActions;
48
+ float m_JumpCoolDownStart;
49
+
50
+ void Awake()
51
+ {
52
+ m_PushBlockSettings = FindObjectOfType<PushBlockWithInputSettings>();
53
+ LazyInitializeActions();
54
+
55
+ // Cache the agent rigidbody
56
+ m_PlayerRb = GetComponent<Rigidbody>();
57
+ }
58
+
59
+ void LazyInitializeActions()
60
+ {
61
+ if (m_PushBlockActions != null)
62
+ {
63
+ return;
64
+ }
65
+
66
+ m_PushBlockActions = new PushBlockActions();
67
+ m_PushBlockActions.Enable();
68
+
69
+ // You can listen to C# events.
70
+ m_PushBlockActions.Movement.jump.performed += JumpOnperformed;
71
+ }
72
+
73
+ void JumpOnperformed(InputAction.CallbackContext callbackContext)
74
+ {
75
+ InnerJump(gameObject.transform);
76
+ }
77
+
78
+ void FixedUpdate()
79
+ {
80
+ // Or you can poll the action itself like we do here.
81
+ InnerMove(gameObject.transform, m_PushBlockActions.Movement.movement.ReadValue<Vector2>());
82
+ if (m_JumpTimeRemaining < 0)
83
+ {
84
+ m_PlayerRb.AddForce(-transform.up * (m_PushBlockSettings.agentJumpForce * 3), ForceMode.Acceleration);
85
+ }
86
+
87
+ m_JumpTimeRemaining -= Time.fixedDeltaTime;
88
+ }
89
+
90
+ void InnerJump(Transform t)
91
+ {
92
+ if (Time.realtimeSinceStartup - m_JumpCoolDownStart > m_PushBlockSettings.agentJumpCoolDown)
93
+ {
94
+ m_JumpTimeRemaining = JumpTime;
95
+ m_PlayerRb.AddForce(t.up * m_PushBlockSettings.agentJumpForce, ForceMode.VelocityChange);
96
+ m_JumpCoolDownStart = Time.realtimeSinceStartup;
97
+ }
98
+ }
99
+
100
+ void InnerMove(Transform t, Vector2 v)
101
+ {
102
+ var forward = CreateForwardVector(v);
103
+ var up = CreateUpVector(v);
104
+ var dirToGo = t.forward * forward;
105
+ var rotateDir = t.up * up;
106
+ t.Rotate(rotateDir, Time.deltaTime * 200f);
107
+ m_PlayerRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed,
108
+ ForceMode.VelocityChange);
109
+ }
110
+
111
+ static float CreateUpVector(Vector2 move)
112
+ {
113
+ return Mathf.Abs(move.x) > Mathf.Abs(move.y) ? move.x : 0f;
114
+ }
115
+
116
+ static float CreateForwardVector(Vector2 move)
117
+ {
118
+ return Mathf.Abs(move.y) > Mathf.Abs(move.x) ? move.y : 0f;
119
+ }
120
+
121
+ /// <summary>
122
+ /// This is the implementation of the <see cref="IInputActionAssetProvider"/> for this class. We need
123
+ /// both the <see cref="GetInputActionAsset"/> and the <see cref="IInputActionCollection2"/> if you are
124
+ /// listening to C# events, Unity Events, or receiving Messages from the Input System Package as those callbacks
125
+ /// are set up through the generated <see cref="IInputActionCollection2"/>.
126
+ /// </summary>
127
+ /// <returns></returns>
128
+ public (InputActionAsset, IInputActionCollection2) GetInputActionAsset()
129
+ {
130
+ LazyInitializeActions();
131
+ return (m_PushBlockActions.asset, m_PushBlockActions);
132
+ }
133
+ }
134
+
16
135
  ### 試したこと
17
136
 
18
137
  参考にさせていただいておりますサイトはこちらです。