Unity 机器学习代理工具包 (ML-Agents) 是一个开源项目,它使游戏和模拟能够作为训练智能代理的环境。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

54 行
1.8 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MLAgents
{
/// <summary>
/// This class contains logic for locomotion agents with joints which might make contact with the ground.
/// By attaching this as a component to those joints, their contact with the ground can be used as either
/// an observation for that agent, and/or a means of punishing the agent for making undesirable contact.
/// </summary>
[DisallowMultipleComponent]
public class GroundContact : MonoBehaviour
{
[HideInInspector] public Agent agent;
[Header("Ground Check")] public bool agentDoneOnGroundContact; // Whether to reset agent on ground contact.
public bool penalizeGroundContact; // Whether to penalize on contact.
public float groundContactPenalty; // Penalty amount (ex: -1).
public bool touchingGround;
private const string Ground = "ground"; // Tag of ground object.
/// <summary>
/// Check for collision with ground, and optionally penalize agent.
/// </summary>
void OnCollisionEnter(Collision col)
{
if (col.transform.CompareTag(Ground))
{
touchingGround = true;
if (penalizeGroundContact)
{
agent.SetReward(groundContactPenalty);
}
if (agentDoneOnGroundContact)
{
agent.Done();
}
}
}
/// <summary>
/// Check for end of ground collision and reset flag appropriately.
/// </summary>
void OnCollisionExit(Collision other)
{
if (other.transform.CompareTag(Ground))
{
touchingGround = false;
}
}
}
}