浏览代码

Collaborative walljump

/MLA-1734-demo-provider
Ervin Teng 4 年前
当前提交
e05e0aef
共有 9 个文件被更改,包括 2166 次插入11 次删除
  1. 22
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  2. 1001
      Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpCollabArea.prefab
  3. 7
      Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpCollabArea.prefab.meta
  4. 1001
      Project/Assets/ML-Agents/Examples/WallJump/Scenes/WallJumpCollab.unity
  5. 7
      Project/Assets/ML-Agents/Examples/WallJump/Scenes/WallJumpCollab.unity.meta
  6. 50
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallAreaScoring.cs
  7. 11
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallAreaScoring.cs.meta
  8. 67
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpCollabAgent.cs
  9. 11
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpCollabAgent.cs.meta

22
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs


public class WallJumpAgent : Agent
{
// Depending on this value, the wall will have different height
int m_Configuration;
protected int m_Configuration;
// Brain to use when no wall is present
public NNModel noWallBrain;
// Brain to use when a jumpable wall is present

public GameObject ground;
public GameObject spawnArea;
Bounds m_SpawnAreaBounds;
protected Bounds m_SpawnAreaBounds;
Rigidbody m_ShortBlockRb;
Rigidbody m_AgentRb;
Material m_GroundMaterial;
Renderer m_GroundRenderer;
WallJumpSettings m_WallJumpSettings;
protected Rigidbody m_ShortBlockRb;
protected Rigidbody m_AgentRb;
protected Material m_GroundMaterial;
protected Renderer m_GroundRenderer;
protected WallJumpSettings m_WallJumpSettings;
public float jumpingTime;
public float jumpTime;

string m_SmallWallBehaviorName = "SmallWallJump";
string m_BigWallBehaviorName = "BigWallJump";
EnvironmentParameters m_ResetParams;
protected EnvironmentParameters m_ResetParams;
public override void Initialize()
{

/// <returns>The Enumerator to be used in a Coroutine.</returns>
/// <param name="mat">The material to be swapped.</param>
/// <param name="time">The time the material will remain.</param>
IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
protected IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
{
m_GroundRenderer.material = mat;
yield return new WaitForSeconds(time); //wait for 2 sec

}
// Detect when the agent hits the goal
void OnTriggerStay(Collider col)
protected virtual void OnTriggerStay(Collider col)
{
if (col.gameObject.CompareTag("goal") && DoGroundCheck(true))
{

/// If 1: Small wall and smallWallBrain.
/// Other : Tall wall and BigWallBrain.
/// </param>
void ConfigureAgent(int config)
protected virtual void ConfigureAgent(int config)
{
var localScale = wall.transform.localScale;
if (config == 0)

1001
Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpCollabArea.prefab
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpCollabArea.prefab.meta


fileFormatVersion: 2
guid: ff1a5743be49d43f08378dcd76451821
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/WallJump/Scenes/WallJumpCollab.unity
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/WallJump/Scenes/WallJumpCollab.unity.meta


fileFormatVersion: 2
guid: 136090e065a8f48bfb97ea3083893d8a
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

50
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallAreaScoring.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallAreaScoring : MonoBehaviour
{
public GameObject[] agents;
WallJumpSettings m_WallJumpSettings;
Renderer m_GroundRenderer;
Material m_GroundMaterial;
protected IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
{
m_GroundRenderer.material = mat;
yield return new WaitForSeconds(time); //wait for 2 sec
m_GroundRenderer.material = m_GroundMaterial;
}
public void Start()
{
m_WallJumpSettings = FindObjectOfType<WallJumpSettings>();
m_GroundRenderer = GetComponent<Renderer>();
m_GroundMaterial = m_GroundRenderer.material;
}
public void WinCondition()
{
foreach (var agent in agents)
{
WallJumpCollabAgent agentScript = agent.GetComponent<WallJumpCollabAgent>();
agentScript.SetReward(1f);
agentScript.EndEpisode();
}
StartCoroutine(
GoalScoredSwapGroundMaterial(m_WallJumpSettings.goalScoredMaterial, 1f));
}
public void LoseCondition()
{
foreach (var agent in agents)
{
WallJumpCollabAgent agentScript = agent.GetComponent<WallJumpCollabAgent>();
agentScript.SetReward(-1f);
agentScript.EndEpisode();
}
StartCoroutine(
GoalScoredSwapGroundMaterial(m_WallJumpSettings.failMaterial, .2f));
}
}

11
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallAreaScoring.cs.meta


fileFormatVersion: 2
guid: ad7213222795741f5b6ca2b332f16da9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

67
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpCollabAgent.cs


//Put this script on your blue cube.
using System.Collections;
using UnityEngine;
using Unity.MLAgents;
using Unity.Barracuda;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using Unity.MLAgentsExamples;
public class WallJumpCollabAgent : WallJumpAgent
{
Vector3 m_InitialPosition;
WallAreaScoring m_Scoring;
public override void Initialize()
{
m_WallJumpSettings = FindObjectOfType<WallJumpSettings>();
m_Scoring = ground.GetComponent<WallAreaScoring>();
m_Configuration = 5;
m_AgentRb = GetComponent<Rigidbody>();
// m_ShortBlockRb = shortBlock.GetComponent<Rigidbody>();
m_SpawnAreaBounds = spawnArea.GetComponent<Collider>().bounds;
m_GroundRenderer = ground.GetComponent<Renderer>();
m_GroundMaterial = m_GroundRenderer.material;
m_InitialPosition = transform.localPosition;
spawnArea.SetActive(false);
m_ResetParams = Academy.Instance.EnvironmentParameters;
}
public override void OnEpisodeBegin()
{
transform.localPosition = m_InitialPosition;
m_Configuration = 5;
m_AgentRb.velocity = default(Vector3);
}
public override void OnActionReceived(ActionBuffers actionBuffers)
{
MoveAgent(actionBuffers.DiscreteActions);
if (!Physics.Raycast(m_AgentRb.position, Vector3.down, 20))
{
m_Scoring.LoseCondition();
}
}
protected override void ConfigureAgent(int config)
{
var localScale = wall.transform.localScale;
var height = m_ResetParams.GetWithDefault("big_wall_height", 8);
localScale = new Vector3(
localScale.x,
height,
localScale.z);
wall.transform.localScale = localScale;
}
// Detect when the agent hits the goal
protected override void OnTriggerStay(Collider col)
{
if (col.gameObject.CompareTag("goal") && DoGroundCheck(true))
{
m_Scoring.WinCondition();
}
}
}

11
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpCollabAgent.cs.meta


fileFormatVersion: 2
guid: 2cdbc0d9a64fe4b12a3ed4b81a151117
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存