Advertisement
Guest User

TrainingArea.cs

a guest
Mar 30th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using UnityEngine;
  6. using MLAgents;
  7. using TMPro;
  8. using Random = UnityEngine.Random;
  9.  
  10. public class TrainingArea : MonoBehaviour
  11. {
  12. public MainAgent mainAgent;
  13.  
  14. public GameObject goal;
  15.  
  16. public TextMeshPro rewardText;
  17.  
  18. public Pickup pickupPrefab;
  19.  
  20. private List<GameObject> pickupList;
  21.  
  22. private void Start()
  23. {
  24. ResetArea();
  25. }
  26.  
  27. private void Update()
  28. {
  29. rewardText.text = mainAgent.GetCumulativeReward().ToString("0.00");
  30. }
  31.  
  32.  
  33. public void ResetArea()
  34. {
  35. RemovePickups();
  36. PlaceAgent();
  37. PlaceGoal();
  38. InstantiatePickup(4);
  39. }
  40.  
  41. public void RemoveSpecificPickup(GameObject pickup)
  42. {
  43. pickupList.Remove(pickup);
  44. Destroy(pickup);
  45. }
  46.  
  47. public int PickupsRemaining
  48. {
  49. get { return pickupList.Count; }
  50. }
  51.  
  52. public static Vector3 RandomPosition(Vector3 topLeft, Vector3 bottomRight)
  53. {
  54. return new Vector3(Random.Range(topLeft.x, bottomRight.x), 0.5f, Random.Range(topLeft.z, bottomRight.z));
  55. }
  56.  
  57. private void RemovePickups()
  58. {
  59. if (pickupList != null)
  60. {
  61. for (int i = 0; i < pickupList.Count; i++)
  62. {
  63. if (pickupList[i] != null)
  64. {
  65. Destroy(pickupList[i]);
  66. }
  67. }
  68. }
  69. pickupList = new List<GameObject>();
  70. }
  71.  
  72. private void PlaceAgent()
  73. {
  74. Rigidbody rb = mainAgent.GetComponent<Rigidbody>();
  75. rb.velocity = Vector3.zero;
  76. rb.angularVelocity = Vector3.zero;
  77. mainAgent.transform.position = RandomPosition(transform.position + new Vector3(-7, 0.5f, 7), transform.position + new Vector3(7, 0.5f, -7));
  78. mainAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
  79. }
  80.  
  81. private void PlaceGoal()
  82. {
  83. Rigidbody rb = goal.GetComponent<Rigidbody>();
  84. rb.velocity = Vector3.zero;
  85. rb.angularVelocity = Vector3.zero;
  86. goal.transform.position = RandomPosition(transform.position + new Vector3(-20f, 0.5f, 20f), transform.position + new Vector3(20f, 0.5f, -20f));
  87. goal.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
  88. }
  89.  
  90. private void InstantiatePickup(int count)
  91. {
  92. for (int i = 0; i < count; i++)
  93. {
  94. GameObject pickupObject = Instantiate<GameObject>(pickupPrefab.gameObject);
  95. pickupObject.transform.position = RandomPosition(transform.position + new Vector3(-20f, 0.5f, 20f),
  96. transform.position + new Vector3(20f, 0.5f, -20f));
  97. pickupObject.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
  98.  
  99. pickupObject.transform.SetParent(transform);
  100.  
  101. pickupList.Add(pickupObject);
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement