Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using UnityEngine;
- using MLAgents;
- using TMPro;
- using Random = UnityEngine.Random;
- public class TrainingArea : MonoBehaviour
- {
- public MainAgent mainAgent;
- public GameObject goal;
- public TextMeshPro rewardText;
- public Pickup pickupPrefab;
- private List<GameObject> pickupList;
- private void Start()
- {
- ResetArea();
- }
- private void Update()
- {
- rewardText.text = mainAgent.GetCumulativeReward().ToString("0.00");
- }
- public void ResetArea()
- {
- RemovePickups();
- PlaceAgent();
- PlaceGoal();
- InstantiatePickup(4);
- }
- public void RemoveSpecificPickup(GameObject pickup)
- {
- pickupList.Remove(pickup);
- Destroy(pickup);
- }
- public int PickupsRemaining
- {
- get { return pickupList.Count; }
- }
- public static Vector3 RandomPosition(Vector3 topLeft, Vector3 bottomRight)
- {
- return new Vector3(Random.Range(topLeft.x, bottomRight.x), 0.5f, Random.Range(topLeft.z, bottomRight.z));
- }
- private void RemovePickups()
- {
- if (pickupList != null)
- {
- for (int i = 0; i < pickupList.Count; i++)
- {
- if (pickupList[i] != null)
- {
- Destroy(pickupList[i]);
- }
- }
- }
- pickupList = new List<GameObject>();
- }
- private void PlaceAgent()
- {
- Rigidbody rb = mainAgent.GetComponent<Rigidbody>();
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- mainAgent.transform.position = RandomPosition(transform.position + new Vector3(-7, 0.5f, 7), transform.position + new Vector3(7, 0.5f, -7));
- mainAgent.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
- }
- private void PlaceGoal()
- {
- Rigidbody rb = goal.GetComponent<Rigidbody>();
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- goal.transform.position = RandomPosition(transform.position + new Vector3(-20f, 0.5f, 20f), transform.position + new Vector3(20f, 0.5f, -20f));
- goal.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
- }
- private void InstantiatePickup(int count)
- {
- for (int i = 0; i < count; i++)
- {
- GameObject pickupObject = Instantiate<GameObject>(pickupPrefab.gameObject);
- pickupObject.transform.position = RandomPosition(transform.position + new Vector3(-20f, 0.5f, 20f),
- transform.position + new Vector3(20f, 0.5f, -20f));
- pickupObject.transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
- pickupObject.transform.SetParent(transform);
- pickupList.Add(pickupObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement