In-game real time reward chart, free camera, result viewer and etc...
In-game real time reward chart, result viewer, free camera, kill bonus reward(not so good. set 0 to unuse it) Fix ray-sensor throw error when initialization issue. Fix ray-sensor info panel face to wrong position.
This commit is contained in:
@@ -9,8 +9,6 @@ using UnityEditor;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using XCharts;
|
||||
using XCharts.Runtime;
|
||||
using System.Linq;
|
||||
|
||||
/*主要ML-Agent控制*/
|
||||
@@ -20,6 +18,7 @@ public class AgentWithGun : Agent
|
||||
public GameObject EnviromentObj;
|
||||
public GameObject EnemyContainerObj;
|
||||
public GameObject thisAgentObj;
|
||||
public GameObject EnvironmentUIControlObj;
|
||||
public Transform thisAgent;
|
||||
public Camera thisCam;
|
||||
public CharacterController PlayerController;
|
||||
@@ -37,7 +36,7 @@ public class AgentWithGun : Agent
|
||||
[Tooltip("Agent Do shoot action reward")]
|
||||
public float shootRewardDefault = -0.1f;
|
||||
[Tooltip("Agent Do shoot action but gun is not read")]
|
||||
public float shootWithoutReadyRewardDefault = -1.0f;
|
||||
public float shootWithoutReadyRewardDefault = -0.15f;
|
||||
[Tooltip("Hit Enemy reward")]
|
||||
public float hitRewardDefault = 2.0f;
|
||||
[Tooltip("Episode Win reward")]
|
||||
@@ -46,6 +45,8 @@ public class AgentWithGun : Agent
|
||||
public float loseRewardDefault = -0.05f;
|
||||
[Tooltip("Enemy down reward")]
|
||||
public float killRewardDefault = 5.0f;
|
||||
[Tooltip("Kill bonus reward stack to nothing happend reward")]
|
||||
public float killBonusRewardDefault = 1.0f;
|
||||
|
||||
[Header("Env")]
|
||||
public bool lockMouse = false;
|
||||
@@ -76,6 +77,7 @@ public class AgentWithGun : Agent
|
||||
public bool defaultTPCamera = true;
|
||||
private bool gunReadyToggle = true;
|
||||
private RaySensors rayScript;
|
||||
private EnviromentUIControl EnvUICon;
|
||||
|
||||
[System.NonSerialized] public float minEnemyAreaX;
|
||||
[System.NonSerialized] public float maxEnemyAreaX;
|
||||
@@ -93,11 +95,14 @@ public class AgentWithGun : Agent
|
||||
[System.NonSerialized] public float winReward;
|
||||
[System.NonSerialized] public float loseReward;
|
||||
[System.NonSerialized] public float killReward;
|
||||
[System.NonSerialized] public float killBonusReward;
|
||||
[System.NonSerialized] public int remainTime;
|
||||
[System.NonSerialized] public int finishedState;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
EnvUICon = EnvironmentUIControlObj.GetComponent<EnviromentUIControl>();
|
||||
rayScript = GetComponent<RaySensors>();
|
||||
// give default Reward to Reward value will be used.
|
||||
nonReward = nonRewardDefault;
|
||||
@@ -107,6 +112,7 @@ public class AgentWithGun : Agent
|
||||
winReward = winRewardDefault;
|
||||
loseReward = loseRewardDefault;
|
||||
killReward = killRewardDefault;
|
||||
killBonusReward = killBonusRewardDefault;
|
||||
//initialize remainTime
|
||||
remainTime = (int)(timeLimit - Time.time + startTime);
|
||||
|
||||
@@ -399,6 +405,9 @@ public class AgentWithGun : Agent
|
||||
for (int i = 0; i < enemyKillCount; i++)
|
||||
{
|
||||
epreward += killReward;
|
||||
nonReward += killBonusReward;
|
||||
shootReward += killBonusReward;
|
||||
shootWithoutReadyReward += killBonusReward;
|
||||
}
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
@@ -428,6 +437,16 @@ public class AgentWithGun : Agent
|
||||
randomInitAgent();
|
||||
randomInitEnemys(enemyNum);
|
||||
nowEnemyNum = getEnemyNum(); // Reset Enemy number
|
||||
// give default Reward to Reward value will be used.
|
||||
EnvUICon.initChart();
|
||||
nonReward = nonRewardDefault;
|
||||
shootReward = shootRewardDefault;
|
||||
shootWithoutReadyReward = shootWithoutReadyRewardDefault;
|
||||
hitReward = hitRewardDefault;
|
||||
winReward = winRewardDefault;
|
||||
loseReward = loseRewardDefault;
|
||||
killReward = killRewardDefault;
|
||||
killBonusReward = killBonusRewardDefault;
|
||||
}
|
||||
|
||||
// ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS
|
||||
@@ -475,20 +494,21 @@ public class AgentWithGun : Agent
|
||||
float thisRoundReward = rewardCalculate();
|
||||
|
||||
//判断结束
|
||||
int finished = checkFinish();
|
||||
if (finished == 1)
|
||||
finishedState = checkFinish();
|
||||
if (finishedState == 1)
|
||||
{
|
||||
//Win Finished
|
||||
EP += 1;
|
||||
EnvUICon.updateChart(winReward);
|
||||
SetReward(winReward);
|
||||
Debug.Log("reward = " + winReward);
|
||||
EndEpisode();
|
||||
}
|
||||
else if (finished == 2)
|
||||
else if (finishedState == 2)
|
||||
{
|
||||
//Lose Finished
|
||||
|
||||
EP += 1;
|
||||
EnvUICon.updateChart(loseReward);
|
||||
SetReward(loseReward);
|
||||
Debug.Log("reward = " + loseReward);
|
||||
EndEpisode();
|
||||
@@ -497,6 +517,7 @@ public class AgentWithGun : Agent
|
||||
{
|
||||
// game not over yet
|
||||
step += 1;
|
||||
EnvUICon.updateChart(thisRoundReward);
|
||||
SetReward(thisRoundReward);
|
||||
Debug.Log("reward = " + thisRoundReward);
|
||||
}
|
||||
@@ -506,7 +527,6 @@ public class AgentWithGun : Agent
|
||||
// 控制调试
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
//
|
||||
//-------------------BUILD
|
||||
ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
|
||||
ActionSegment<int> discreteActions = actionsOut.DiscreteActions;
|
||||
|
||||
@@ -9,6 +9,7 @@ using UnityEngine;
|
||||
public class RaySensors : MonoBehaviour
|
||||
{
|
||||
public Camera agentCam;
|
||||
public Camera TPSCam;
|
||||
public Material lineMeterial;
|
||||
public GameObject rayInfoPrefab;
|
||||
public GameObject agentCanvas;
|
||||
@@ -125,7 +126,7 @@ public class RaySensors : MonoBehaviour
|
||||
turnOffLine(thisLineRenderer, rayColor);
|
||||
}
|
||||
// drawRay in game
|
||||
if (showInGameRayInfo) thisRayInfoUI.updateInfo(rayInfoText, rayInfoPosition, rayColor);
|
||||
if (showInGameRayInfo) thisRayInfoUI.updateInfo(rayInfoText, rayInfoPosition, rayColor,TPSCam);
|
||||
// Show log
|
||||
if (showDebugRay) Debug.DrawRay(ray.origin, ray.direction * viewDistance, rayColor); // drawRay in debug
|
||||
// Debug.Log(ray.origin + ray.direction);
|
||||
|
||||
@@ -5,21 +5,33 @@ using TMPro;
|
||||
public class rayInfoUI : MonoBehaviour
|
||||
{
|
||||
TextMeshProUGUI infoText;
|
||||
|
||||
bool infoTextReady = false;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
infoText = transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>();
|
||||
try
|
||||
{
|
||||
infoText = transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>();
|
||||
infoTextReady = true;
|
||||
}
|
||||
catch (UnityException)
|
||||
{
|
||||
infoTextReady = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void updateInfo(string info,Vector3 infoPosition, Color infoColor)
|
||||
public void updateInfo(string info,Vector3 infoPosition, Color infoColor,Camera facetoCamera)
|
||||
{
|
||||
if (!infoTextReady)
|
||||
{
|
||||
infoText = transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>();
|
||||
}
|
||||
infoText.text = info;
|
||||
infoText.color = infoColor;
|
||||
transform.position = infoPosition;
|
||||
Vector3 v = Camera.main.transform.position - infoPosition;
|
||||
Vector3 v = facetoCamera.transform.position - infoPosition;
|
||||
v.x = v.z = 0.0f;
|
||||
transform.LookAt(Camera.main.transform.position - v);
|
||||
transform.LookAt(facetoCamera.transform.position - v);
|
||||
transform.Rotate(0, 180, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user