浏览代码

WIP 2d scene

/2D-exploration-raycast
Chris Elion 5 年前
当前提交
59317314
共有 8 个文件被更改,包括 839 次插入32 次删除
  1. 74
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea2D.prefab
  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. 27
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs
  6. 11
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAcademy.cs.meta
  7. 336
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs
  8. 11
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJump2DAgent.cs.meta

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


- component: {fileID: 61607684435006894}
- component: {fileID: 50038960445382884}
- component: {fileID: 114028198358633208}
- component: {fileID: 114107366108349864}
- component: {fileID: 114735272992145842}
- component: {fileID: 114325747366330126}
m_Layer: 0
m_Name: Agent
m_TagString: Untagged

- component: {fileID: 61175182912136580}
m_Layer: 0
m_Name: Wall (2)
m_TagString: Untagged
m_TagString: wall
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!1 &1643424632278004
GameObject:
m_ObjectHideFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!1 &1782507024130018
GameObject:
m_ObjectHideFlags: 0

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1014421213912728}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -2.6181662, y: -8.92453, z: 0.171875}
m_LocalRotation: {x: -0, y: 0.0523359, z: -0, w: 0.9986295}
m_LocalPosition: {x: -4.6418, y: 1.1930466, z: -2}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4323441479098572}

m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1661633189295650}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -3.301165, y: -4.462265, z: 0.171875}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalPosition: {x: -4.66, y: -6.18, z: -1}
m_LocalScale: {x: 0.75, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4323441479098572}
m_RootOrder: 9

m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1847453981338936}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.2237248, y: 0.1605815, z: -0.171875}
m_LocalPosition: {x: 149.99193, y: 56.688637, z: -24.85398}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 4234453863133892}

m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1048363797171572}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 2.8913646, y: 7.2853303, z: 0.171875}
m_LocalPosition: {x: 4.97, y: -8.25, z: 0.171875}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4323441479098572}

m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_CollisionDetection: 1
m_Constraints: 0
--- !u!61 &61005056756666034
BoxCollider2D:

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1782507024130018}
m_Enabled: 1
m_Enabled: 0
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0

m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 4
vectorObservationSize: 2
vectorActionSize: 03000000030000000300000002000000
vectorActionSize: 0300000002000000
m_Model: {fileID: 11400000, guid: 0468bf44b1efd4992b6bf22cadb50d89, type: 3}
m_Model: {fileID: 0}
m_BehaviorType: 0
m_BehaviorType: 1
--- !u!114 &114107366108349864
--- !u!114 &114325747366330126
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}

m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 676fca959b8ee45539773905ca71afa1, type: 3}
m_Script: {fileID: 11500000, guid: f67c7e722ba14acd9153bb4488bff6e4, type: 3}
m_Name:
m_EditorClassIdentifier:
sensorName: RayPerceptionSensor
detectableTags:
- wall
- ground
- goal
raysPerDirection: 3
maxRayDegrees: 70
sphereCastRadius: 0.45
rayLength: 20
observationStacks: 1
rayHitColor: {r: 1, g: 0, b: 0, a: 1}
rayMissColor: {r: 1, g: 1, b: 1, a: 1}
useWorldPositions: 0
--- !u!114 &114735272992145842
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1014421213912728}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: faebf211ad16b4cc48451ec57da7e31c, type: 3}
m_Name:
m_EditorClassIdentifier:
agentParameters:

numberOfActionsBetweenDecisions: 5
noWallBrain: {fileID: 11400000, guid: fb2ce36eb40b6480e94ea0b5d7573e47, type: 3}
smallWallBrain: {fileID: 11400000, guid: fb2ce36eb40b6480e94ea0b5d7573e47, type: 3}
bigWallBrain: {fileID: 11400000, guid: 0468bf44b1efd4992b6bf22cadb50d89, type: 3}
ground: {fileID: 0}
spawnArea: {fileID: 0}
goal: {fileID: 0}
ground: {fileID: 1782507024130018}
spawnArea: {fileID: 1661633189295650}
goal: {fileID: 1048363797171572}
jumpingTime: 0
jumpingTime: 0.18
jumpTime: 0.2
fallingForce: 111
hitGroundColliders:

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 2100000, guid: a1daf31cdf41e484ca9ac33a5c6f524a, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0

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:

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:

336
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()
{
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;
}
}
}

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:
正在加载...
取消
保存