Ball은 RandomBall() 함수에서 Board 테두리에서 생성돼 Agent를 향해 날라가는데 약간의 노이즈를 섞습니다.
테두리에 충돌한 Ball은 Collider가 OnTrigger되어 wall일경우 RandomBall()함수를 이용 동작을 반복하게 됩니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DBall : MonoBehaviour
{
GameObject Agent;
float ball_speed;
float ball_random;
float board_radius;
public void SetBall(GameObject Agent_, float ball_speed_, float ball_random_, float board_radius_)
{
Agent = Agent_;
ball_speed = ball_speed_;
ball_random = ball_random_;
board_radius = board_radius_ - 0.55f; //테두리 보다 살짝 안쪽에 생성되도록
RandomBall();
}
///<summary>
/// 공의 초기 위치, 각도, 속도를 결정하는 함수
///</summary>
public void RandomBall()
{
float theta = Random.Range(0, 2 * Mathf.PI);
// Area테두리에서 theta각도의 위취로 설정
transform.localPosition = new Vector3(board_radius * Mathf.Cos(theta), 0.25f, board_radius * Mathf.Sin(theta));
// Agent를 바라보는 방향이나 약간 랜덤을 섞는다.
float randomAngle = Mathf.Atan2(Agent.transform.localPosition.z - transform.localPosition.z,
Agent.transform.localPosition.x - transform.localPosition.x) + Random.Range(-ball_random, ball_random);
// 기본 볼의 속도에 랜덤을 섞는다
float randomSpeed = ball_speed + Random.Range(-0.5f * ball_random, 0.5f * ball_random);
Rigidbody rig = GetComponent<Rigidbody>();
rig.velocity = new Vector3(randomSpeed * Mathf.Cos(randomAngle), 0, randomSpeed * Mathf.Sin(randomAngle));
}
///<summary>
/// 충돌처리 함수
///</summary>
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("wall"))
{
RandomBall();
}
}
}
'강화학습 > ML-Agent Unity' 카테고리의 다른 글
Dodge 환경설정및 환경빌드 (0) | 2025.04.28 |
---|---|
Agent Script - Dodge (0) | 2025.04.28 |
Dodge Scene 스크립트 작성 (0) | 2025.04.28 |
Dodge 씬 구성 - Unity ML Agent (0) | 2025.04.28 |
2차 환경제작 (0) | 2024.08.19 |