Download Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs from ashish2244/ppo-SnowballTarget: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/ashish2244/ppo-SnowballTarget/resolve/main/Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
- Command line
-
hf download hf://ashish2244/ppo-SnowballTarget/Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
-
curl -L -o Ball3DHardAgent.cs https://huggingface.co/ashish2244/ppo-SnowballTarget/resolve/main/Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
2.95 kB
| using UnityEngine; | |
| using Unity.MLAgents; | |
| using Unity.MLAgents.Actuators; | |
| using Unity.MLAgents.Sensors.Reflection; | |
| public class Ball3DHardAgent : Agent | |
| { | |
| [] | |
| public GameObject ball; | |
| Rigidbody m_BallRb; | |
| EnvironmentParameters m_ResetParams; | |
| public override void Initialize() | |
| { | |
| m_BallRb = ball.GetComponent<Rigidbody>(); | |
| m_ResetParams = Academy.Instance.EnvironmentParameters; | |
| SetResetParameters(); | |
| } | |
| [] | |
| Vector2 Rotation | |
| { | |
| get | |
| { | |
| return new Vector2(gameObject.transform.rotation.z, gameObject.transform.rotation.x); | |
| } | |
| } | |
| [] | |
| Vector3 PositionDelta | |
| { | |
| get | |
| { | |
| return ball.transform.position - gameObject.transform.position; | |
| } | |
| } | |
| public override void OnActionReceived(ActionBuffers actionBuffers) | |
| { | |
| var continuousActions = actionBuffers.ContinuousActions; | |
| var actionZ = 2f * Mathf.Clamp(continuousActions[0], -1f, 1f); | |
| var actionX = 2f * Mathf.Clamp(continuousActions[1], -1f, 1f); | |
| if ((gameObject.transform.rotation.z < 0.25f && actionZ > 0f) || | |
| (gameObject.transform.rotation.z > -0.25f && actionZ < 0f)) | |
| { | |
| gameObject.transform.Rotate(new Vector3(0, 0, 1), actionZ); | |
| } | |
| if ((gameObject.transform.rotation.x < 0.25f && actionX > 0f) || | |
| (gameObject.transform.rotation.x > -0.25f && actionX < 0f)) | |
| { | |
| gameObject.transform.Rotate(new Vector3(1, 0, 0), actionX); | |
| } | |
| if ((ball.transform.position.y - gameObject.transform.position.y) < -2f || | |
| Mathf.Abs(ball.transform.position.x - gameObject.transform.position.x) > 3f || | |
| Mathf.Abs(ball.transform.position.z - gameObject.transform.position.z) > 3f) | |
| { | |
| SetReward(-1f); | |
| EndEpisode(); | |
| } | |
| else | |
| { | |
| SetReward(0.1f); | |
| } | |
| } | |
| public override void OnEpisodeBegin() | |
| { | |
| gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f); | |
| gameObject.transform.Rotate(new Vector3(1, 0, 0), Random.Range(-10f, 10f)); | |
| gameObject.transform.Rotate(new Vector3(0, 0, 1), Random.Range(-10f, 10f)); | |
| m_BallRb.linearVelocity = new Vector3(0f, 0f, 0f); | |
| ball.transform.position = new Vector3(Random.Range(-1.5f, 1.5f), 4f, Random.Range(-1.5f, 1.5f)) | |
| + gameObject.transform.position; | |
| } | |
| public void SetBall() | |
| { | |
| //Set the attributes of the ball by fetching the information from the academy | |
| m_BallRb.mass = m_ResetParams.GetWithDefault("mass", 1.0f); | |
| var scale = m_ResetParams.GetWithDefault("scale", 1.0f); | |
| ball.transform.localScale = new Vector3(scale, scale, scale); | |
| } | |
| public void SetResetParameters() | |
| { | |
| SetBall(); | |
| } | |
| } | |