Our first project with ML-Agents!!!!! We have the Unity project created, and some auxiliar scripts.
This is the script created for our target. Te responsible to move the object to a new position when the agent reach the target, after wait a few seconds.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Premio : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("mlagent"))
{
Invoke("MoverPosicionInicial", 4);
}
}
private void MoverPosicionInicial()
{
bool posicionEncontrada = false;
int intentos = 100;
Vector3 posicionPotencial = Vector3.zero;
while (!posicionEncontrada && intentos >= 0)
{
intentos--;
posicionPotencial = new Vector3(
transform.parent.position.x + UnityEngine.Random.Range(-3f, 3f),
0.555f,
transform.parent.position.z + UnityEngine.Random.Range(-3f, 3f));
//just check the collisions
Collider[] colliders = Physics.OverlapSphere(posicionPotencial, 0.05f);
if (colliders.Length == 0)
{
transform.position = posicionPotencial;
posicionEncontrada = true;
}
}
}
}
And now the MoveBall script. We will use it only to test the mechanics of the game. Although we will reuse the same code later, for the agent script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBall : MonoBehaviour
{
Rigidbody _rb = null;
public float speed = 400;
void Start()
{
_rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
_rb.AddForce(movement * speed * Time.deltaTime);
}
}