๐ท ์ค์ต 6 (2)
- ์ถ๊ตฌ
- ์ด์ฐ๊ฐ
๐ท ํธ๋ ์ด๋ ํ๊ฒฝ ๊ตฌ์ถ
- ํ๊ทธ ์ถ๊ฐ
- ๊ฐ ์ค๋ธ์ ํธ์ ํ๊ทธ ๋ถ์ฌ
- ์ปดํฌ๋ํธ ์ถ๊ฐ : Ray Perception Sensor 3D
- Sensor Name : RayFront : ์ผ์๋ฅผ ์ฌ๋ฌ๊ฐ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ด๋ฆ์ด ์ค๋ณต๋๋ฉด ์๋จ
- Detectable Tags : ์ธ์ํ ์ค๋ธ์ ํธ์ ํ๊ทธ ์ถ๊ฐ
- Ray per Director : 4 : ๋ ์ด์ ๊ฐ์ : 4 * 2 + 1 = 9๊ฐ
- Max Ray Degrees : 80 : ๋ ์ด์ ๊ฐ๊ฒฉ
- Ray Length : 40 : Agent๊ฐ ๊ฒฝ๊ธฐ์ฅ ๋์ ์์๋๋ ๋ฐ๋ํธ๊น์ง ๋ ์ด๊ฐ ๋ฟ๋๋ก, ์ฌ๊ฐ์ง๋๊ฐ ์๋๋ก ์กฐ์
- ์ปดํฌ๋ํธ copy
- Agent ์๋์ ๋น ๊ฒ์์ค๋ธ์ ํธ ์ถ๊ฐ : BackRay
- ์ปดํฌ๋ํธ paste : Ray Perception Sensor 3D
- Rotation : 0, 180, 0
- Sensor Name : RayBack
- Ray per Director : 2
- Max Ray Degrees : 90
- Ray Hit Color : Green
- Apply All
- Agent > Agent_Blue ์ด๋ฆ๋ณ๊ฒฝ
- ๋ณต์ฌ > Agent_Red ์ด๋ฆ๋ณ๊ฒฝ
- Team : Red
- TMP_Pro ์ํฌํธ
- ์บ๋ฒ์ค ์ถ๊ฐ
- Render Mode : World Space
- Rect Transform : Reset
- Pos Y : 0.2
- Width : 30
- Height : 15
- Rotation : 90, 0, 0
- ์บ๋ฒ์ค ์๋์ TMP_Text ์ค๋ธ์ ํธ ์ถ๊ฐ > Text - Blue Score๋ก ์ด๋ฆ ๋ณ๊ฒฝ
- ์ ๋ ฌ : Alt + ์ข์ธกํ๋จ์ ์๋ ๊ฒ
- Pos X : 5
- Width : 10
- Font Size : 3
- Alignment : ์ข์ฐ ๊ฐ์ด๋ฐ, ์ํ ๊ฐ์ด๋ฐ
- Blue Score ๋ณต์ฌ > Text - Red Score๋ก ์ด๋ฆ ๋ณ๊ฒฝ
- ์ ๋ ฌ : Alt + ์ฐ์ธกํ๋จ
๐ถ ๊ณจ๋์ ๊ณต์ด ๋ฟ์ผ๋ฉด ๋์ ์ฒ๋ฆฌ
- ์คํฌ๋ฆฝํธ ์์ฑ : BallCtrl.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;
public class BallCtrl : MonoBehaviour
{
public Agent[] players;
// ๊ฐ ํ์ ์ ์
private int blueScore, redScore;
public TMP_Text blueScoreText, redScoreText;
void Start()
{
}
}
- Soccer Ball์ BallCtrl ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- players : Blue, Red ์์ ๋ง์ถฐ์ ์ฐ๊ฒฐ
- TMP_Text๋ ์ฐ๊ฒฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;
public class BallCtrl : MonoBehaviour
{
public Agent[] players;
private new Rigidbody rigidbody;
// ๊ฐ ํ์ ์ ์
private int blueScore, redScore;
public TMP_Text blueScoreText, redScoreText;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
void InitBall()
{
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
// ์ถ๊ตฌ๊ณต์ ์์น๋ฅผ ์ด๊ธฐํ
transform.localPosition = new Vector3(0.0f, 1.0f, 0.0f);
}
private void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("BLUE_GOAL"))
{
// ๋ ๋ํ ์ค์ฝ์ด +1์
redScoreText.text = (++redScore).ToString();
// ๋ ๋ํ ๋ฆฌ์๋ +1.0f
// ๋ธ๋ฃจํ ๋ฆฌ์๋ -1.0f
InitBall();
}
if (other.collider.CompareTag("RED_GOAL"))
{
// ๋ธ๋ฃจํ ์ค์ฝ์ด +1์
blueScoreText.text = (++blueScore).ToString();
// ๋ ๋ํ ๋ฆฌ์๋ -1.0f
// ๋ธ๋ฃจํ ๋ฆฌ์๋ +1.0f
InitBall();
}
}
}
- ๊ณจ๋์ ๊ณต์ ๋ฃ์ผ๋ฉด ์ค์ฝ์ด ๋์
๐ถ ๋ฆฌ์๋ ๋ถ์ฌ ๋ฐ ํ์ต์ข ๋ฃ ์ธํ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;
public class BallCtrl : MonoBehaviour
{
public Agent[] players;
private new Rigidbody rigidbody;
// ๊ฐ ํ์ ์ ์
private int blueScore, redScore;
public TMP_Text blueScoreText, redScoreText;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
void InitBall()
{
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
// ์ถ๊ตฌ๊ณต์ ์์น๋ฅผ ์ด๊ธฐํ
transform.localPosition = new Vector3(0.0f, 1.0f, 0.0f);
}
private void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("BLUE_GOAL"))
{
// ๋ ๋ํ ์ค์ฝ์ด +1์
redScoreText.text = (++redScore).ToString();
// ๋ ๋ํ ๋ฆฌ์๋ +1.0f
players[1].AddReward(+1.0f);
// ๋ธ๋ฃจํ ๋ฆฌ์๋ -1.0f
players[0].AddReward(-1.0f);
// ๋ณผ ์ด๊ธฐํ
InitBall();
players[0].EndEpisode(); // ๋ธ๋ฃจํ ํ์ต์ข
๋ฃ
players[1].EndEpisode(); // ๋ ๋ํ ํ์ต์ข
๋ฃ
}
if (other.collider.CompareTag("RED_GOAL"))
{
// ๋ธ๋ฃจํ ์ค์ฝ์ด +1์
blueScoreText.text = (++blueScore).ToString();
// ๋ ๋ํ ๋ฆฌ์๋ -1.0f
players[1].AddReward(-1.0f);
// ๋ธ๋ฃจํ ๋ฆฌ์๋ +1.0f
players[0].AddReward(+1.0f);
// ๋ณผ ์ด๊ธฐํ
InitBall();
players[0].EndEpisode(); // ๋ธ๋ฃจํ ํ์ต์ข
๋ฃ
players[1].EndEpisode(); // ๋ ๋ํ ํ์ต์ข
๋ฃ
}
}
}
๐ท ํธ๋ ์ด๋
- poca ํด๋ ์์ ์๋ SoccerTwos.yaml ๋ณต์ฌ
- behavior ์ด๋ฆ ๋์ผํ๊ฒ ๋ณ๊ฒฝ
- Stage ํ๋ฆฌํนํ
- ๋ณต์ฌ
- ํธ๋ ์ด๋ ์์
- ์ ๋ํฐ play
'Unity > ML-Agents' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
06. ML-Agents - Soccer (1) (1) | 2021.08.02 |
---|---|
05. ML-Agents - Imitation Learning (0) | 2021.08.02 |
04. ML-Agents - Camera Sensor (0) | 2021.08.01 |
03. ML-Agents - Ray Perception Sensor 3D (0) | 2021.07.30 |
02. ML-Agents - position,rigidbody ๊ด์ธก (0) | 2021.07.30 |