比较提交

...
此合并请求有变更与目标分支冲突。
/UnitySDK/ProjectSettings/ProjectVersion.txt
/UnitySDK/Assets/ML-Agents/Scripts/Agent.cs

2 次代码提交

作者 SHA1 备注 提交日期
Chris Elion 5aa420ae do raycasting during heuristic 5 年前
Chris Elion 59317314 WIP 2d scene 5 年前
共有 12 个文件被更改,包括 1996 次插入7 次删除
  1. 2
      UnitySDK/ProjectSettings/ProjectVersion.txt
  2. 11
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
  3. 394
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump2D.unity
  4. 7
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump2D.unity.meta
  5. 3
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJump.png
  6. 191
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJump.png.meta
  7. 8
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea2D.prefab.meta
  8. 1001
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea2D.prefab
  9. 27
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs
  10. 11
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs.meta
  11. 11
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs.meta
  12. 337
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs

2
UnitySDK/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2017.4.32f1
m_EditorVersion: 2017.4.35f1

11
UnitySDK/Assets/ML-Agents/Scripts/Agent.cs


{
m_Id = gameObject.GetInstanceID();
var academy = FindObjectOfType<Academy>();
if (academy == null)
{
throw new UnityAgentsException(
"No Academy Component could be found in the scene.");
}
academy.LazyInitialization();
OnEnableHelper(academy);

m_Info = new AgentInfo();
m_Action = new AgentAction();
sensors = new List<ISensor>();
if (academy == null)
{
throw new UnityAgentsException(
"No Academy Component could be found in the scene.");
}
academy.AgentSetStatus += SetStatus;
academy.AgentResetIfDone += ResetIfDone;

394
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump2D.unity


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
OcclusionCullingSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 8
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: 0.01
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
m_DefaultReflectionMode: 0
m_DefaultReflectionResolution: 128
m_ReflectionBounces: 1
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.49641287, b: 0.5748173, a: 1}
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
m_GIWorkflowMode: 0
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_TemporalCoherenceThreshold: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 1
m_LightmapEditorSettings:
serializedVersion: 9
m_Resolution: 2
m_BakeResolution: 40
m_TextureWidth: 1024
m_TextureHeight: 1024
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 0
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500
m_PVRBounces: 2
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringMode: 1
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
m_PVRFilteringGaussRadiusAO: 2
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
agentSlope: 45
agentClimb: 0.4
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
minRegionArea: 2
manualCellSize: 0
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &358464456
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 358464460}
- component: {fileID: 358464459}
- component: {fileID: 358464458}
- component: {fileID: 358464457}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &358464457
AudioListener:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 358464456}
m_Enabled: 1
--- !u!124 &358464458
Behaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 358464456}
m_Enabled: 1
--- !u!20 &358464459
Camera:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 358464456}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 0
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!4 &358464460
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 358464456}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &374918265
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 374918267}
- component: {fileID: 374918266}
m_Layer: 0
m_Name: Directional Light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &374918266
Light:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 374918265}
m_Enabled: 1
serializedVersion: 8
m_Type: 1
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &374918267
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 374918265}
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1001 &753967149
Prefab:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.x
value: 149.99193
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.y
value: 56.688637
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.z
value: -24.85398
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4323441479098572, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_RootOrder
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114028198358633208, guid: 22d0a69521a5d484fb770a8d59d21af3,
type: 2}
propertyPath: m_BrainParameters.vectorObservationSize
value: 2
objectReference: {fileID: 0}
- target: {fileID: 114028198358633208, guid: 22d0a69521a5d484fb770a8d59d21af3,
type: 2}
propertyPath: m_Model
value:
objectReference: {fileID: 0}
- target: {fileID: 114028198358633208, guid: 22d0a69521a5d484fb770a8d59d21af3,
type: 2}
propertyPath: m_BehaviorType
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1492283146608182, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1661633189295650, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4301162835029576, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.z
value: -2
objectReference: {fileID: 0}
- target: {fileID: 1430302707972218, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_TagString
value: wall
objectReference: {fileID: 0}
- target: {fileID: 61609502669604710, guid: 22d0a69521a5d484fb770a8d59d21af3,
type: 2}
propertyPath: m_Enabled
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4559655702709942, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.y
value: -8.25
objectReference: {fileID: 0}
- target: {fileID: 4559655702709942, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
propertyPath: m_LocalPosition.x
value: 4.97
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 22d0a69521a5d484fb770a8d59d21af3, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1056584310
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1056584312}
- component: {fileID: 1056584311}
m_Layer: 0
m_Name: Academy
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1056584311
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1056584310}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2f028d72f6ead4692afdcdb0e8b25682, type: 3}
m_Name:
m_EditorClassIdentifier:
agentRunSpeed: 3
agentJumpHeight: 2.75
goalScoredMaterial: {fileID: 2100000, guid: df32cc593804f42df97464dc455057b8, type: 2}
failMaterial: {fileID: 2100000, guid: a1daf31cdf41e484ca9ac33a5c6f524a, type: 2}
gravityMultiplier: 2.5
agentJumpVelocity: 777
agentJumpVelocityMaxChange: 10
--- !u!4 &1056584312
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1056584310}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

7
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump2D.unity.meta


fileFormatVersion: 2
guid: 309c2117028d547fca15f10f6e451820
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

3
UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJump.png
文件差异内容过多而无法显示
查看文件

191
UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJump.png.meta


fileFormatVersion: 2
guid: c80802de675b1d54d851f0490c472ed6
TextureImporter:
fileIDToRecycleName:
21300000: Ground
21300002: WallJump_1
21300004: Player
21300006: WallCorner
21300008: Spawn
21300010: Goal
21300012: ShortBox
externalObjects: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -1
wrapU: 1
wrapV: 1
wrapW: -1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 2
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 32
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites:
- serializedVersion: 2
name: Ground
rect:
serializedVersion: 2
x: 37
y: 352
width: 641
height: 641
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: WallJump_1
rect:
serializedVersion: 2
x: 704
y: 352
width: 16
height: 641
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: Player
rect:
serializedVersion: 2
x: 543
y: 303
width: 34
height: 34
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: WallCorner
rect:
serializedVersion: 2
x: 705
y: 320
width: 16
height: 16
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: Spawn
rect:
serializedVersion: 2
x: 80
y: 58
width: 534
height: 214
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: Goal
rect:
serializedVersion: 2
x: 640
y: 198
width: 101
height: 107
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
- serializedVersion: 2
name: ShortBox
rect:
serializedVersion: 2
x: 640
y: 80
width: 96
height: 96
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape: []
tessellationDetail: 0
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:

8
UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea2D.prefab.meta


fileFormatVersion: 2
guid: 22d0a69521a5d484fb770a8d59d21af3
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 100100000
userData:
assetBundleName:
assetBundleVariant:

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

27
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs


using UnityEngine;
using MLAgents;
public class WallJump2DAcademy : Academy
{
[Header("Specific to WallJump")]
public float agentRunSpeed;
public float agentJumpHeight;
//when a goal is scored the ground will use this material for a few seconds.
public Material goalScoredMaterial;
//when fail, the ground will use this material for a few seconds.
public Material failMaterial;
[HideInInspector]
//use ~3 to make things less floaty
public float gravityMultiplier = 2.5f;
[HideInInspector]
public float agentJumpVelocity = 777;
[HideInInspector]
public float agentJumpVelocityMaxChange = 10;
// Use this for initialization
public override void InitializeAcademy()
{
Physics.gravity *= gravityMultiplier;
}
}

11
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs.meta


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

11
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs.meta


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

337
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs


//Put this script on your blue cube.
using System.Collections;
using UnityEngine;
using MLAgents;
using Barracuda;
public class WallJump2DAgent : Agent
{
// Depending on this value, the wall will have different height
int m_Configuration;
public GameObject ground;
public GameObject spawnArea;
Bounds m_SpawnAreaBounds;
public GameObject goal;
public GameObject shortBlock;
public GameObject wall;
Rigidbody2D m_AgentRb;
Material m_GroundMaterial;
Renderer m_GroundRenderer;
WallJump2DAcademy m_Academy;
public float jumpingTime;
public float jumpTime;
// This is a downward force applied when falling to make jumps look
// less floaty
public float fallingForce;
// Use to check the coliding objects
public Collider2D[] hitGroundColliders = new Collider2D[3];
Vector3 m_JumpTargetPos;
Vector3 m_JumpStartingPos;
public override void InitializeAgent()
{
m_Academy = FindObjectOfType<WallJump2DAcademy>();
m_Configuration = Random.Range(0, 5);
m_AgentRb = GetComponent<Rigidbody2D>();
m_SpawnAreaBounds = spawnArea.GetComponent<Collider2D>().bounds;
m_GroundRenderer = ground.GetComponent<Renderer>();
m_GroundMaterial = m_GroundRenderer.material;
spawnArea.SetActive(false);
}
// Begin the jump sequence
public void Jump()
{
jumpingTime = 0.2f;
m_JumpStartingPos = m_AgentRb.position;
}
/// <summary>
/// Does the ground check.
/// </summary>
/// <returns><c>true</c>, if the agent is on the ground,
/// <c>false</c> otherwise.</returns>
/// <param name="smallCheck"></param>
public bool DoGroundCheck(bool smallCheck)
{
return true;
// if (!smallCheck)
// {
// hitGroundColliders = new Collider2D[3];
// var o = gameObject;
// Physics2D.OverlapBoxNonAlloc(
// o.transform.position + new Vector3(0, -0.05f, 0),
// new Vector3(0.95f / 2f, 0.5f, 0.95f / 2f),
// hitGroundColliders,
// o.transform.rotation);
// var grounded = false;
// foreach (var col in hitGroundColliders)
// {
// if (col != null && col.transform != transform &&
// (col.CompareTag("walkableSurface") ||
// col.CompareTag("block") ||
// col.CompareTag("wall")))
// {
// grounded = true; //then we're grounded
// break;
// }
// }
// return grounded;
// }
// else
{
RaycastHit2D hit = Physics2D.Raycast(
(Vector2)transform.position + new Vector2(0, -0.05f),
-Vector2.up,
1f
);
if (hit.collider != null &&
(hit.collider.CompareTag("walkableSurface") ||
hit.collider.CompareTag("block") ||
hit.collider.CompareTag("wall"))
&& hit.normal.y > 0.95f)
{
return true;
}
return false;
}
}
/// <summary>
/// Moves a rigidbody towards a position smoothly.
/// </summary>
/// <param name="targetPos">Target position.</param>
/// <param name="rb">The rigidbody to be moved.</param>
/// <param name="targetVel">The velocity to target during the
/// motion.</param>
/// <param name="maxVel">The maximum velocity posible.</param>
void MoveTowards(
Vector2 targetPos, Rigidbody2D rb, float targetVel, float maxVel)
{
var moveToPos = targetPos - rb.worldCenterOfMass;
var velocityTarget = Time.fixedDeltaTime * targetVel * moveToPos;
if (float.IsNaN(velocityTarget.x) == false)
{
rb.velocity = Vector2.MoveTowards(rb.velocity, velocityTarget, maxVel);
}
}
public override void CollectObservations()
{
var agentPos = m_AgentRb.position - (Vector2) ground.transform.position;
AddVectorObs(agentPos / 20f);
//AddVectorObs(DoGroundCheck(true) ? 1 : 0);
}
/// <summary>
/// Gets a random spawn position in the spawningArea.
/// </summary>
/// <returns>The random spawn position.</returns>
public Vector2 GetRandomSpawnPos()
{
var randomPosX = Random.Range(-m_SpawnAreaBounds.extents.x,
m_SpawnAreaBounds.extents.x);
var randomPosY = Random.Range(-m_SpawnAreaBounds.extents.y,
m_SpawnAreaBounds.extents.y);
var transformPos = (Vector2) spawnArea.transform.position;
var randomSpawnPos = transformPos + new Vector2(randomPosX, randomPosY);
return randomSpawnPos;
}
/// <summary>
/// Chenges the color of the ground for a moment
/// </summary>
/// <returns>The Enumerator to be used in a Coroutine</returns>
/// <param name="mat">The material to be swaped.</param>
/// <param name="time">The time the material will remain.</param>
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 MoveAgent(float[] act)
{
AddReward(-0.0005f);
var smallGrounded = DoGroundCheck(true);
var largeGrounded = DoGroundCheck(false);
var dirToGo = Vector2.zero;
var dirToGoSideAction = (int)act[0];
var jumpAction = (int)act[1];
if (dirToGoSideAction != 0)
{
Debug.Log($"dirToGoSideAction={dirToGoSideAction}, transform.right={transform.right} transform.up={transform.up} transform.fwd={transform.forward}");
}
if (dirToGoSideAction == 1)
dirToGo = (largeGrounded ? 1f : 0.5f) * -0.6f * transform.right;
else if (dirToGoSideAction == 2)
dirToGo = (largeGrounded ? 1f : 0.5f) * 0.6f * transform.right;
if (jumpAction == 1)
if ((jumpingTime <= 0f) && smallGrounded)
{
Jump();
}
//transform.Rotate(rotateDir, Time.fixedDeltaTime * 300f);
m_AgentRb.AddForce(dirToGo * m_Academy.agentRunSpeed,
ForceMode2D.Impulse);
if (jumpingTime > 0f)
{
m_JumpTargetPos =
new Vector2(m_AgentRb.position.x,
m_JumpStartingPos.y + m_Academy.agentJumpHeight) + dirToGo;
MoveTowards(m_JumpTargetPos, m_AgentRb, m_Academy.agentJumpVelocity,
m_Academy.agentJumpVelocityMaxChange);
}
if (!(jumpingTime > 0f) && !largeGrounded)
{
m_AgentRb.AddForce(
Vector3.down * fallingForce, ForceMode2D.Force);
}
jumpingTime -= Time.fixedDeltaTime;
}
public override void AgentAction(float[] vectorAction)
{
MoveAgent(vectorAction);
// TODO can't happen in 2D?
if (!Physics2D.Raycast(m_AgentRb.position, Vector2.down, 20))
{
Done();
SetReward(-1f);
StartCoroutine(
GoalScoredSwapGroundMaterial(m_Academy.failMaterial, .5f));
}
}
public override float[] Heuristic()
{
GenerateSensorData();
bool pressed = false;
var action = new float[4];
if (Input.GetKey(KeyCode.D))
{
pressed = true;
action[0] = 2f;
}
if (Input.GetKey(KeyCode.A))
{
pressed = true;
action[0] = 1f;
}
if (Input.GetKey(KeyCode.Space))
{
pressed = true;
action[1] = 1.0f;
}
if (pressed)
{
Debug.Log($"({action[0]}, {action[1]}, {action[2]}, {action[3]})");
}
return action;
}
// Detect when the agent hits the goal
void OnTriggerStay2D(Collider2D col)
{
if (col.gameObject.CompareTag("goal") && DoGroundCheck(true))
{
SetReward(1f);
Done();
StartCoroutine(
GoalScoredSwapGroundMaterial(m_Academy.goalScoredMaterial, 2));
}
}
//Reset the orange block position
// void ResetBlock(Rigidbody blockRb)
// {
// blockRb.transform.position = GetRandomSpawnPos();
// blockRb.velocity = Vector3.zero;
// blockRb.angularVelocity = Vector3.zero;
// }
public override void AgentReset()
{
Debug.Log("AgentReset");
transform.localPosition = new Vector3(4 * (Random.value - 0.5f), 1, -2);
//var randomPos2d = GetRandomSpawnPos();
//var randomPos = new Vector3(randomPos2d.x, randomPos2d.y, transform.localPosition.z);
//transform.localPosition = randomPos;
m_Configuration = Random.Range(0, 5);
m_AgentRb.velocity = default(Vector3);
}
void FixedUpdate()
{
if (m_Configuration != -1)
{
ConfigureAgent(m_Configuration);
m_Configuration = -1;
}
}
/// <summary>
/// Configures the agent. Given an integer config, the wall will have
/// different height and a different brain will be assigned to the agent.
/// </summary>
/// <param name="config">Config.
/// If 0 : No wall and noWallBrain.
/// If 1: Small wall and smallWallBrain.
/// Other : Tall wall and BigWallBrain. </param>
void ConfigureAgent(int config)
{
if (wall == null)
{
// TODO handle reset here
return;
}
var localScale = wall.transform.localScale;
if (config == 0)
{
localScale = new Vector3(
localScale.x,
m_Academy.FloatProperties.GetPropertyWithDefault("no_wall_height", 0),
localScale.z);
wall.transform.localScale = localScale;
}
else if (config == 1)
{
localScale = new Vector3(
localScale.x,
m_Academy.FloatProperties.GetPropertyWithDefault("small_wall_height", 4),
localScale.z);
wall.transform.localScale = localScale;
}
else
{
var min = m_Academy.FloatProperties.GetPropertyWithDefault("big_wall_min_height", 8);
var max = m_Academy.FloatProperties.GetPropertyWithDefault("big_wall_max_height", 8);
var height = min + Random.value * (max - min);
localScale = new Vector3(
localScale.x,
height,
localScale.z);
wall.transform.localScale = localScale;
}
}
}
正在加载...
取消
保存