지정된 개수의 랜덤한 특성을 가진 볼들을 만들어줍니다. 각 Area의 환경도 랜덤하게 초기화해줍니다.

자료구조를 사용하기 위해 using System.Collections.Generic; 추가합니다.

    public GameObject Agent; 에이전트
    public GameObject Ball; 볼
    public GameObject Env; GameBoard
    public GameObject Wall; 벽

    List<GameObject> balls = new List<GameObject>(); 공리스트
    List<DBall> ballScripts = new List<DBall>(); BallScript 리스트

    public float boardRadius = 6.0f;  보드크기
    public float ballSpeed = 3.0f;  공스피드
    public int ballNums = 15; 공개수
    public float ballRandom = 0.2f; 공에 무작위성을 부여하기 위한 변수
    public float agentSpeed = 3.0f; 에이전트의 속도

 

InitBall() 공에대한 초기화를 실행합니다. 공 게임오브젝트를 생성합니다.

ResetEnv() 환경값을 초기화하고 InitBall()을 실행합니다. Agent.cs에서 불리워집니다.

using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;

public class DArea : MonoBehaviour
{
    public GameObject Agent;
    public GameObject Ball;
    public GameObject Env;
    public GameObject Wall;

    List<GameObject> balls = new List<GameObject>();
    List<DBall> ballScripts = new List<DBall>();

    public float boardRadius = 6.0f;
    public float ballSpeed = 3.0f;
    public int ballNums = 15;
    public float ballRandom = 0.2f;
    public float agentSpeed = 3.0f;

    DAgent agentScript = null;
    EnvironmentParameters m_ResetParams = null;

    void Start()
    {
        m_ResetParams = Academy.Instance.EnvironmentParameters;
        agentScript = Agent.GetComponent<DAgent>();
        InitBall();
    }

    public void InitBall()
    {
        ballScripts.Clear();

        for (int i = 0; i < balls.Count; i++)
            Destroy(balls[i]);

        balls.Clear();

        for (int i = 0; i < ballNums; i++)
        {
            GameObject b = Instantiate(Ball, Env.transform);
            DBall script = b.GetComponent<DBall>();
            script.SetBall(Agent, ballSpeed, ballRandom, boardRadius);
            balls.Add(b);
            ballScripts.Add(script);
        }
    }

    public void ResetEnv()
    {
        if (null == m_ResetParams)
            m_ResetParams = Academy.Instance.EnvironmentParameters;

        boardRadius = m_ResetParams.GetWithDefault("boardRadius", boardRadius);
        ballSpeed = m_ResetParams.GetWithDefault("ballSpeed", ballSpeed);
        ballNums = (int)m_ResetParams.GetWithDefault("ballNums", ballNums);
        ballRandom = m_ResetParams.GetWithDefault("ballRandom", ballRandom);
        agentSpeed = m_ResetParams.GetWithDefault("agentSpeed", agentSpeed);

        if (null == agentScript)
            agentScript = Agent.GetComponent<DAgent>();

        agentScript.SetAgentSpeed(agentSpeed);
        Wall.transform.localScale = new Vector3(boardRadius, 10f, boardRadius);

        InitBall();
    }
}

+ Recent posts