diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab index 25a3948e..f4c01c03 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarAgentGridCollab.prefab @@ -427,8 +427,8 @@ NavMeshAgent: m_Enabled: 1 m_AgentTypeID: -1372625422 m_Radius: 37.5 - m_Speed: 100 - m_Acceleration: 500 + m_Speed: 200 + m_Acceleration: 1000 avoidancePriority: 50 m_AngularSpeed: 120 m_StoppingDistance: 0 diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab index f37769d4..da0af2ec 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Prefabs/CarCatchingArea.prefab @@ -238,7 +238,7 @@ PrefabInstance: - target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a, type: 3} propertyPath: customNavigationGoal.x - value: -212 + value: 281 objectReference: {fileID: 0} - target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a, type: 3} @@ -248,7 +248,7 @@ PrefabInstance: - target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a, type: 3} propertyPath: customNavigationGoal.z - value: -231 + value: -152 objectReference: {fileID: 0} - target: {fileID: 8880364502379087430, guid: a431228058383324287911e110279f5a, type: 3} diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs index 9f24b4b9..c4b41f56 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarAgent.cs @@ -1,5 +1,4 @@ -using System; -using UnityEngine; +using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; @@ -23,8 +22,12 @@ protected override void Awake() void Start() { - var goal = useRandomNavigationGoal ? customNavigationGoal : customNavigationGoal; - GetComponent().SetDestination(customNavigationGoal); + var localY = transform.localPosition.y; // Attention: local Y + + var navigationGoal = useRandomNavigationGoal + ? carCatchingEnvController.GetRandomPos() + new Vector3(0f, localY, 0f) + : customNavigationGoal; + GetComponent().SetDestination(navigationGoal); } /// diff --git a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs index 6ef0106f..0db4e1ff 100644 --- a/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs +++ b/Project/Assets/ML-Agents/Examples/CarCatching/Scripts/CarCatchingEnvController.cs @@ -48,22 +48,41 @@ public class CarInfo // private int m_ResetTimer; - void Start() + protected void Awake() { // Get the ground's bounds areaBounds = ground.GetComponent().bounds; m_CarCatchingSettings = FindObjectOfType(); + } + + void Start() + { foreach (var item in AgentsList) { - item.StartingPos = item.Agent.transform.position; - item.StartingRot = item.Agent.transform.rotation; - item.StartingScale = item.Agent.transform.localScale; - item.GoalPosition = item.Agent.transform.position; + var itemTrans = item.Agent.transform; + item.StartingPos = itemTrans.position; + item.StartingRot = itemTrans.rotation; + item.StartingScale = itemTrans.localScale; + item.GoalPosition = itemTrans.position; } ResetScene(); } + /// + /// Use the ground's bounds to pick a random local position in the area bounds. Make sure the Y value + /// is 0, X and Z value lies in [-areaBounds.extents.x(z), areaBounds.extents.x(z)], respectively. + /// + public Vector3 GetRandomPos() + { + var randomPosX = Random.Range(-areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier, + areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier); + + var randomPosZ = Random.Range(-areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier, + areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier); + return new Vector3(randomPosX, 0f, randomPosZ); + } + /// /// Use the ground's bounds to pick a random spawn position. The requirements is that the car's collider /// doesn't collide with any wall's collider @@ -76,13 +95,8 @@ public Vector3 GetRandomSpawnPos(Quaternion rot) var agentHalfExtents = AgentsList[0].StartingScale * 0.5f; while (foundNewSpawnLocation == false) { - var randomPosX = Random.Range(-areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier, - areaBounds.extents.x * m_CarCatchingSettings.spawnAreaMarginMultiplier); - - var randomPosZ = Random.Range(-areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier, - areaBounds.extents.z * m_CarCatchingSettings.spawnAreaMarginMultiplier); - //Global Position - randomSpawnPos = ground.transform.position + new Vector3(randomPosX, localY, randomPosZ); + //Global Position = ground position + x,z local position + y + randomSpawnPos = ground.transform.position + GetRandomPos() + new Vector3(0f, localY, 0f); if (Physics.CheckBox(randomSpawnPos, agentHalfExtents, rot) == false) { foundNewSpawnLocation = true;