| |
| using UnityEngine; |
| using Unity.MLAgents; |
| using Unity.MLAgents.Actuators; |
|
|
| public class PushAgentCollab : Agent |
| { |
|
|
| private PushBlockSettings m_PushBlockSettings; |
| private Rigidbody m_AgentRb; |
|
|
| protected override void Awake() |
| { |
| base.Awake(); |
| m_PushBlockSettings = FindAnyObjectByType<PushBlockSettings>(); |
| } |
|
|
| public override void Initialize() |
| { |
| |
| m_AgentRb = GetComponent<Rigidbody>(); |
| } |
|
|
| |
| |
| |
| public void MoveAgent(ActionSegment<int> act) |
| { |
| var dirToGo = Vector3.zero; |
| var rotateDir = Vector3.zero; |
|
|
| var action = act[0]; |
|
|
| switch (action) |
| { |
| case 1: |
| dirToGo = transform.forward * 1f; |
| break; |
| case 2: |
| dirToGo = transform.forward * -1f; |
| break; |
| case 3: |
| rotateDir = transform.up * 1f; |
| break; |
| case 4: |
| rotateDir = transform.up * -1f; |
| break; |
| case 5: |
| dirToGo = transform.right * -0.75f; |
| break; |
| case 6: |
| dirToGo = transform.right * 0.75f; |
| break; |
| } |
| transform.Rotate(rotateDir, Time.fixedDeltaTime * m_PushBlockSettings.agentRotationSpeed); |
| m_AgentRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed, |
| ForceMode.VelocityChange); |
| } |
|
|
| |
| |
| |
| public override void OnActionReceived(ActionBuffers actionBuffers) |
|
|
| { |
| |
| MoveAgent(actionBuffers.DiscreteActions); |
| } |
|
|
| public override void Heuristic(in ActionBuffers actionsOut) |
| { |
| var discreteActionsOut = actionsOut.DiscreteActions; |
| if (Input.GetKey(KeyCode.D)) |
| { |
| discreteActionsOut[0] = 3; |
| } |
| else if (Input.GetKey(KeyCode.W)) |
| { |
| discreteActionsOut[0] = 1; |
| } |
| else if (Input.GetKey(KeyCode.A)) |
| { |
| discreteActionsOut[0] = 4; |
| } |
| else if (Input.GetKey(KeyCode.S)) |
| { |
| discreteActionsOut[0] = 2; |
| } |
| } |
| } |
|
|