Browse Source

waypoints and getup model added

animation-test
Cailean Finn 8 months ago
parent
commit
0487423a9e
  1. BIN
      Assets/2_Chair/Brains/Getup.onnx
  2. 16
      Assets/2_Chair/Brains/Getup.onnx.meta
  3. 36
      Assets/2_Chair/Chair.prefab
  4. 42
      Assets/2_Chair/Target_Cube.prefab
  5. 267
      Assets/6_Scenes/Beep.unity
  6. 27
      Assets/7_Scripts/Agent/TargetControllerWalker.cs
  7. 80
      Assets/7_Scripts/Agent/Walker.cs
  8. 115
      Assets/7_Scripts/Model Swapping/ModelSwap.cs
  9. 64
      Assets/7_Scripts/Model Swapping/SwapModelOnContact.cs
  10. 11
      Assets/7_Scripts/Model Swapping/SwapModelOnContact.cs.meta
  11. 8
      Assets/7_Scripts/Waypoint.meta
  12. 53
      Assets/7_Scripts/Waypoint/Waypoint.cs
  13. 2
      Assets/7_Scripts/Waypoint/Waypoint.cs.meta
  14. 109
      Assets/7_Scripts/Waypoint/WaypointManager.cs
  15. 2
      Assets/7_Scripts/Waypoint/WaypointManager.cs.meta
  16. 2
      Assets/ML-Agents/Timers/Beep_timers.json

BIN
Assets/2_Chair/Brains/Getup.onnx

Binary file not shown.

16
Assets/2_Chair/Brains/Getup.onnx.meta

@ -0,0 +1,16 @@
fileFormatVersion: 2
guid: 4564a8c073c3e134b96ce1698dbae60b
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 683b6cb6d0a474744822c888b46772c9, type: 3}
optimizeModel: 1
forceArbitraryBatchSize: 1
treatErrorsAsWarnings: 0
importMode: 1
weightsTypeMode: 0
activationTypeMode: 0

36
Assets/2_Chair/Chair.prefab

@ -2063,12 +2063,12 @@ MonoBehaviour:
maxStep: 0
hasUpgradedFromAgentParameters: 1
MaxStep: 5000
m_SelectedBrain: 4
m_SelectedBrain: 0
m_TargetWalkingSpeed: 8
rWalkSpeedEachEpisode: 0
m_RandomiseYRotation: 0
m_RandomiseXYZRotation: 0
m_ModelSwap: 0
m_ModelSwap: 1
m_ProximitySwapper: 0
m_SwitchModelAfterFalling: 0
m_StepCountAtLastMeter: 0
@ -2127,14 +2127,19 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5777259271730693716}
m_Enabled: 0
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4d4f1f9b90b1b3949a25ccabbf8429d4, type: 3}
m_Name:
m_EditorClassIdentifier:
m_InitialModel: {fileID: 5022602860645237092, guid: 6c625593ea946484995cf5e4ec762dc7,
m_InitialModel: {fileID: 5022602860645237092, guid: 1dc2dd8bd62c1ce47b13f1740b15e522,
type: 3}
nnModelList: []
nnModelList:
- {fileID: 5022602860645237092, guid: 1dc2dd8bd62c1ce47b13f1740b15e522, type: 3}
- {fileID: 5022602860645237092, guid: 00d0acaeb52a3894a9b8ca8c664b12db, type: 3}
- {fileID: 5022602860645237092, guid: 4e2e22c8058641f4f943af2d50b147f6, type: 3}
- {fileID: 5022602860645237092, guid: 4564a8c073c3e134b96ce1698dbae60b, type: 3}
agent: {fileID: 0}
currentModel: 0
m_PastModel: 0
--- !u!1 &6163716254022380080
@ -2566,7 +2571,6 @@ GameObject:
- component: {fileID: 7445707003514573868}
- component: {fileID: 9100050631348364745}
- component: {fileID: 1550985650543268760}
- component: {fileID: 2576851963027223328}
m_Layer: 0
m_Name: Seat
m_TagString: agent
@ -2582,8 +2586,8 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6794064008694923825}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0.017973283, z: -0, w: 0.99983853}
m_LocalPosition: {x: 0.59, y: -0.6, z: 13.58}
m_LocalRotation: {x: 0, y: -0.4407226, z: 0, w: 0.8976434}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
@ -2666,22 +2670,6 @@ MonoBehaviour:
touchingObject: 0
touchingGround: 0
touchingStairs: 0
--- !u!114 &2576851963027223328
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6794064008694923825}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5c07b818612782c47bc265efc0b072dd, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BalanceSwap: swap_Balance
m_WalkerSwap: swap_Walker
m_SloperSwap: swap_Sloper
m_GetupSwap: swap_Getup
--- !u!1 &7331846505223066426
GameObject:
m_ObjectHideFlags: 0

42
Assets/2_Chair/Target_Cube.prefab

@ -13,7 +13,6 @@ GameObject:
- component: {fileID: 6907201076630297306}
- component: {fileID: 9113762406916612572}
- component: {fileID: 2286595338307215377}
- component: {fileID: 8854810986365377144}
m_Layer: 0
m_Name: Target_Cube
m_TagString: target
@ -48,16 +47,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 688132802d6546041aea7c1df9d5073d, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Reward: 1
m_EndEpisodeOnTouch: 1
modelToChange: {fileID: 6907201076630297306}
modelToUse: {fileID: -6446594586776062275, guid: 0c8f92e6c54a42b4d833d2ace7898bae,
type: 3}
yellow: {fileID: 2100000, guid: 40300f88299ebd94f943e2b83f8f681e, type: 2}
m_Reward: 50
m_EndEpisodeOnTouch: 0
tagToDetect: agent
spawnRadius: 30
respawnIfTouched: 0
respawnIfFallsOffPlatform: 1
respawnIfFallsOffPlatform: 0
fallDistance: 20
chair: {fileID: 0}
onTriggerEnterEvent:
@ -93,7 +88,7 @@ MeshRenderer:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7830662089278843852}
m_Enabled: 1
m_Enabled: 0
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
@ -145,36 +140,9 @@ BoxCollider:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!54 &8854810986365377144
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7830662089278843852}
serializedVersion: 4
m_Mass: 100
m_Drag: 0.05
m_AngularDrag: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_ImplicitCom: 1
m_ImplicitTensor: 1
m_UseGravity: 0
m_IsKinematic: 1
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 3

267
Assets/6_Scenes/Beep.unity

@ -226,17 +226,17 @@ PrefabInstance:
- target: {fileID: 3364236482078802793, guid: 263d8cea838224448bdfd0e71bc2dce4,
type: 3}
propertyPath: m_LocalPosition.x
value: -224.23
value: -222.66632
objectReference: {fileID: 0}
- target: {fileID: 3364236482078802793, guid: 263d8cea838224448bdfd0e71bc2dce4,
type: 3}
propertyPath: m_LocalPosition.y
value: 5.06
value: 5.73497
objectReference: {fileID: 0}
- target: {fileID: 3364236482078802793, guid: 263d8cea838224448bdfd0e71bc2dce4,
type: 3}
propertyPath: m_LocalPosition.z
value: 455.59
value: 457.45187
objectReference: {fileID: 0}
- target: {fileID: 3364236482078802793, guid: 263d8cea838224448bdfd0e71bc2dce4,
type: 3}
@ -1465,6 +1465,77 @@ MonoBehaviour:
m_FocusTarget: {fileID: 3413039205880321923}
m_FocusOffset: 0
m_Profile: {fileID: 11400000, guid: 250442763ac254c8882298f619398df9, type: 2}
--- !u!1 &1201075075
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1201075076}
- component: {fileID: 1201075077}
- component: {fileID: 1201075078}
m_Layer: 0
m_Name: Waypoint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1201075076
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1201075075}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 10.43, y: 0, z: -15.49}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1518800082}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1201075077
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1201075075}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5c81c2052c91a74a8d9fae388fca038, type: 3}
m_Name:
m_EditorClassIdentifier:
m_WaypointPosition: {fileID: 1201075076}
m_WaitTime: 0
m_ModelName: Getup
m_Index: 0
m_Touched: 0
--- !u!65 &1201075078
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1201075075}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &1303368148
GameObject:
m_ObjectHideFlags: 0
@ -1590,41 +1661,6 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalRotation.w
value: 0.8976434
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalRotation.y
value: -0.4407226
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5732046185206465305, guid: f03606b436da1d94e977c8ffa2825bd9,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
@ -1794,6 +1830,57 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 1503240170}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1518800080
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1518800082}
- component: {fileID: 1518800081}
m_Layer: 0
m_Name: Waypoints
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1518800081
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1518800080}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1f83b258daac8e846bed96ffebb253e3, type: 3}
m_Name:
m_EditorClassIdentifier:
m_WaypointList:
- {fileID: 1201075077}
- {fileID: 2070452029}
m_Target: {fileID: 387060610}
m_CurrentWaypoint: {fileID: 1201075077}
--- !u!4 &1518800082
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1518800080}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -232.9, y: 38.5, z: 423.46}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1201075076}
- {fileID: 2070452028}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1665610482
GameObject:
m_ObjectHideFlags: 0
@ -1961,6 +2048,37 @@ Transform:
- {fileID: 1915961234679171070}
m_Father: {fileID: 547380827529143388}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1956142036
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1956142037}
m_Layer: 0
m_Name: '============================ '
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1956142037
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1956142036}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.19631481, y: 32.76503, z: -49.481876}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2024103100
GameObject:
m_ObjectHideFlags: 0
@ -1992,6 +2110,77 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2070452027
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2070452028}
- component: {fileID: 2070452029}
- component: {fileID: 2070452030}
m_Layer: 0
m_Name: Waypoint (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2070452028
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2070452027}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -16.44}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1518800082}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2070452029
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2070452027}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5c81c2052c91a74a8d9fae388fca038, type: 3}
m_Name:
m_EditorClassIdentifier:
m_WaypointPosition: {fileID: 2070452028}
m_WaitTime: 0
m_ModelName:
m_Index: 1
m_Touched: 0
--- !u!65 &2070452030
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2070452027}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &2081823899
GameObject:
m_ObjectHideFlags: 0
@ -16534,3 +16723,5 @@ SceneRoots:
- {fileID: 799276790}
- {fileID: 2024103101}
- {fileID: 479999559}
- {fileID: 1956142037}
- {fileID: 1518800082}

27
Assets/7_Scripts/Agent/TargetControllerWalker.cs

@ -23,17 +23,6 @@ namespace Unity.MLAgentsExamples
[Header("End Episode After Touch")]
public bool m_EndEpisodeOnTouch = false;
[Header("Mesh Settings")]
[SerializeField]
private MeshFilter modelToChange;
[SerializeField]
private Mesh modelToUse;
[SerializeField]
[Header("Controller Colour")]
Material yellow;
[Header("Collider Tag To Detect")]
public string tagToDetect = "agent"; //collider tag to detect
@ -94,7 +83,7 @@ namespace Unity.MLAgentsExamples
/// Moves target to a random position within specified radius.
/// </summary>
public void MoveTargetToRandomPosition()
{
{
var newTargetPos = m_startingPos + (Random.insideUnitSphere * spawnRadius);
newTargetPos.y = m_startingPos.y;
transform.position = newTargetPos;
@ -113,7 +102,7 @@ namespace Unity.MLAgentsExamples
if (m_EndEpisodeOnTouch)
{
chair.EndEpisode();
chair.EndEpisode();
}
onCollisionEnterEvent.Invoke(col);
@ -148,6 +137,18 @@ namespace Unity.MLAgentsExamples
{
if (col.CompareTag(tagToDetect))
{
chair.SetReward(m_Reward);
if (respawnIfTouched)
{
MoveTargetToRandomPosition();
}
if (m_EndEpisodeOnTouch)
{
chair.EndEpisode();
}
onTriggerStayEvent.Invoke(col);
}
}

80
Assets/7_Scripts/Agent/Walker.cs

@ -15,7 +15,7 @@ using System.Xml.Linq;
public class Walker : Agent
{
public enum Brain
{
@ -108,10 +108,13 @@ public class Walker : Agent
var parent = gameObject.transform.parent;
try{
try
{
m_Terrain = parent.GetComponentInChildren<Terrain>();
}catch{
m_Terrain = null;
}
catch
{
m_Terrain = null;
}
// Setup each Body Part
@ -144,16 +147,16 @@ public class Walker : Agent
{
//this.GetComponent<DMTerrain>().Reset();
}
foreach (var bodyPart in m_JointDriveController.bodyPartsDict.Values)
{
bodyPart.Reset(bodyPart);
}
// Apply Random Rotation to Seat
if (m_SelectedBrain == Brain.Getup) { if(m_RandomiseXYZRotation) seat.rotation = Quaternion.Euler(Random.Range(0.0f, 360f), Random.Range(0.0f, 360f), Random.Range(0.0f, 360f)); }
else { if (m_RandomiseYRotation) seat.rotation = Quaternion.Euler(0f, Random.Range(0.0f, 360f), 0f); }
if (m_SelectedBrain == Brain.Getup) { if (m_RandomiseXYZRotation) seat.rotation = Quaternion.Euler(Random.Range(0.0f, 360f), Random.Range(0.0f, 360f), Random.Range(0.0f, 360f)); }
else { if (m_RandomiseYRotation) seat.rotation = Quaternion.Euler(0f, Random.Range(0.0f, 360f), 0f); }
UpdateOrientationObject();
// Set our Walking Speed goal
@ -163,13 +166,13 @@ public class Walker : Agent
// Check if model swapper is enabled, and set model if so
if (m_ModelSwap)
{
m_ModelSwapper.SwitchModel(3, this);
m_ModelSwapper.SwitchModel("Walker", this);
}
m_LastXPosition = (int)GetAverageXPositionFeet();
}
/* Add relevant information for each body part (observations)
*/
public void CollectObservationsBP(BodyPart bp, VectorSensor sensor)
@ -223,7 +226,7 @@ public class Walker : Agent
// Distance of the target relative to the Cube
//sensor.AddObservation(Vector3.Distance(m_OrientationCube.transform.position, target.transform.position));
foreach (var bodyPart in m_JointDriveController.bodyPartsList)
{
CollectObservationsBP(bodyPart, sensor);
@ -280,16 +283,6 @@ public class Walker : Agent
void FixedUpdate()
{
if (m_SelectedBrain == Brain.DMScrambler)
{
if (seat.position.y < -15f)
{
SetReward(-1f);
EndEpisode();
}
}
// Check if model swapper is on
if (m_ModelSwap)
{
@ -306,22 +299,23 @@ public class Walker : Agent
// (3) Distance from ground
// var distFromGround = Mathf.Pow(Mathf.Clamp(Vector3.Distance(seat.transform.position, GameObject.Find("Ground").transform.position) + 0.2f, 0, 1), 2);
if (m_SelectedBrain == Brain.Getup)
{
Vector2 deltaAngle = GetAngleDeltaXZ();
AddReward(deltaAngle.x * deltaAngle.y);
}
else if(m_SelectedBrain == Brain.Balance)
//Debug.Log($"{Mathf.Pow((deltaAngle.x * deltaAngle.y), 2)}");
AddReward(Mathf.Pow((deltaAngle.x * deltaAngle.y), 2) * 0.02f);
}
else if (m_SelectedBrain == Brain.Balance)
{
// Sets reward for the agent based on its stableness, while moving towards a target
Vector2 deltaAngle = GetAngleDeltaXZ();
AddReward(matchSpeedReward * lookAtTargetReward * (deltaAngle.x * deltaAngle.y));
}
else if(m_SelectedBrain == Brain.DMScrambler)
else if (m_SelectedBrain == Brain.DMScrambler)
{
// Normalised Velocity in a certain direciton.
//var seatVelocity = GetAvgVelocitySeat();
@ -356,11 +350,11 @@ public class Walker : Agent
//Debug.Log(DistanceFromTarget(20f));
}
else if(m_SelectedBrain == Brain.Walker)
else if (m_SelectedBrain == Brain.Walker)
{
AddReward(matchSpeedReward * lookAtTargetReward);
}
else if(m_SelectedBrain == Brain.Climber)
else if (m_SelectedBrain == Brain.Climber)
{
//AddReward(matchSpeedReward * lookAtTargetReward * DistanceFromTarget(20f));
AddReward(-0.002f);
@ -372,21 +366,21 @@ public class Walker : Agent
{
if (m_ModelSwap)
{
InputSwitchModel();
//InputSwitchModel();
}
}
private void InputSwitchModel()
{
if (Input.GetKeyDown(KeyCode.O))
{
m_ModelSwapper.SwitchModel(0, this);
m_ModelSwapper.SwitchModel("Walker", this);
}
if (Input.GetKeyDown(KeyCode.P))
{
m_ModelSwapper.SwitchModel(1, this);
m_ModelSwapper.SwitchModel("Stairs", this);
}
}
@ -407,15 +401,15 @@ public class Walker : Agent
{
// Swap to Original Model
m_FinishedSwap = false;
m_ModelSwapper.SwitchModel(4, this);
m_ModelSwapper.SwitchModel("Walker", this);
}
}
}
float DistanceFromTarget(float maxDistance)
float DistanceFromTarget(float maxDistance)
{
float dist = Vector3.Distance(seat.transform.position, target.transform.position);
float normalisedValue = 1- Mathf.InverseLerp(0f, maxDistance, dist);
float dist = Vector3.Distance(seat.transform.position, target.transform.position);
float normalisedValue = 1 - Mathf.InverseLerp(0f, maxDistance, dist);
return Mathf.Pow(normalisedValue, 2);
}
@ -438,10 +432,10 @@ public class Walker : Agent
{
Vector3 velSum = Vector3.zero;
foreach (var item in m_JointDriveController.bodyPartsList)
{
if(item.rb.transform == seat)
if (item.rb.transform == seat)
{
velSum = item.rb.velocity;
break;
@ -473,8 +467,8 @@ public class Walker : Agent
float DistanceFromGround()
{
float normalizedValue = Mathf.Clamp01( (seat.position.y) / 3.0f);
Debug.Log(seat.position.y/3.0f);
float normalizedValue = Mathf.Clamp01((seat.position.y) / 3.0f);
Debug.Log(seat.position.y / 3.0f);
return Mathf.Pow(normalizedValue, 2);
}

115
Assets/7_Scripts/Model Swapping/ModelSwap.cs

@ -16,117 +16,54 @@ public class ModelSwap : MonoBehaviour
public NNModel m_InitialModel;
// public Agent agent;
[HideInInspector] public List<NNModel> nnModelList;
public List<NNModel> nnModelList;
[HideInInspector]
public Walker agent;
[HideInInspector] public int currentModel = 0;
[HideInInspector] public int m_PastModel = 0;
// Path to .onnx files
string m_relPath = string.Empty;
private void Start()
private void OnEnable()
{
// Load Models From Dir
NNFileList();
Waypoint.SwapModelOnWaypointReached += SwapModelOnReachingWaypoint;
}
public void NNFileList()
private void OnDisable()
{
m_relPath = Application.dataPath;
m_relPath = Path.GetDirectoryName(m_relPath);
m_relPath = m_relPath + "\\ModelSwaper\\ModelList";
DirectoryInfo dirInfo = new DirectoryInfo(m_relPath);
FileInfo[] nnList = dirInfo.GetFiles("*.onnx");
// Sort files by creation date
Array.Sort(nnList, delegate (FileInfo x, FileInfo y) { return DateTime.Compare(x.CreationTime, y.CreationTime); });
ConvertNNModels(nnList);
Waypoint.SwapModelOnWaypointReached -= SwapModelOnReachingWaypoint;
}
public void ConvertNNModels(FileInfo[] nnList)
private void Start()
{
foreach (FileInfo element in nnList)
{
var converter = new ONNXModelConverter(true);
byte[] modelData = File.ReadAllBytes(element.FullName.ToString());
Model model = converter.Convert(modelData);
NNModelData modelD = ScriptableObject.CreateInstance<NNModelData>();
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
ModelWriter.Save(writer, model);
modelD.Value = memoryStream.ToArray();
}
modelD.name = "Data";
modelD.hideFlags = HideFlags.HideInHierarchy;
NNModel result = ScriptableObject.CreateInstance<NNModel>();
result.modelData = modelD;
result.name = element.Name;
// Add Model to Model List
nnModelList.Add(result);
}
Debug.Log("Total Number of Models: " + nnModelList.Count);
// Set the inital model to 0
m_InitialModel = nnModelList[currentModel];
// Removed for import errors
//GlobalVars.g_CurrentModel = nnModelList[currentModel].name;
agent = this.GetComponent<Walker>();
}
public void SwitchModel(int modelActive, Agent inst)
// Set Model by name
public void SwitchModel(string modelName, Agent inst)
{
/*
0 = -1 on the model list
1 = +1 on the model list
3 = Starting model
4 = Return to last model active
*/
FindModelByName(modelName);
inst.SetModel("c_Walker", nnModelList[currentModel]);
if (modelActive == 0)
if (modelName.Equals("Walker"))
{
currentModel -= 1;
if(currentModel < 0)
{
currentModel = 0;
}
inst.SetModel("Swap", nnModelList[currentModel]);
//GlobalVars.g_CurrentModel = nnModelList[currentModel].name;
agent.m_SelectedBrain = Walker.Brain.Walker;
}
else if (modelActive == 3)
else if (modelName.Equals("Stairs"))
{
inst.SetModel("Swap", m_InitialModel);
agent.m_SelectedBrain = Walker.Brain.DMScrambler;
}
else if (modelActive == 1)
else if (modelName.Equals("Climber"))
{
currentModel += 1;
if (currentModel > nnModelList.Count - 1)
{
currentModel = nnModelList.Count - 1;
}
inst.SetModel("Swap", nnModelList[currentModel]);
//GlobalVars.g_CurrentModel = nnModelList[currentModel].name;
agent.m_SelectedBrain = Walker.Brain.DMScrambler;
}
else if (modelActive == 4)
else if (modelName.Equals("Getup"))
{
currentModel = m_PastModel;
inst.SetModel("Swap", nnModelList[currentModel]);
//GlobalVars.g_CurrentModel = nnModelList[currentModel].name;
agent.m_SelectedBrain = Walker.Brain.Getup;
}
//Debug.Log("Current Model: " + GlobalVars.g_CurrentModel);
}
// Set Model by name
public void SwitchModel(string modelName, Agent inst)
{
m_PastModel = currentModel;
FindModelByName(modelName);
inst.SetModel("Swap", nnModelList[currentModel]);
//GlobalVars.g_CurrentModel = nnModelList[currentModel].name;
//Debug.Log("Current Model: " + GlobalVars.g_CurrentModel);
Debug.Log("Current Model: " + nnModelList[currentModel].name);
}
private void FindModelByName(string modelName)
@ -134,11 +71,17 @@ public class ModelSwap : MonoBehaviour
int i = 0;
foreach (NNModel element in nnModelList)
{
if(element.name.Equals(modelName+".onnx"))
if (element.name.Equals(modelName))
{
currentModel = i;
return;
}
i++;
}
}
private void SwapModelOnReachingWaypoint(string modelName)
{
SwitchModel(modelName, agent);
}
};

64
Assets/7_Scripts/Model Swapping/SwapModelOnContact.cs

@ -1,64 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
namespace Unity.MLAgentsExamples
{
[DisallowMultipleComponent]
public class SwapModelOnContact : MonoBehaviour
{
[Header("List of Collision Types & Models")]
public string m_BalanceSwap = "swap_Balance";
public string m_WalkerSwap = "swap_Walker";
public string m_SloperSwap = "swap_Sloper";
public string m_GetupSwap = "swap_Getup";
private ModelSwap m_ModelSwapper;
private Agent m_Agent;
void OnEnable()
{
m_ModelSwapper = GetComponentInParent<ModelSwap>();
m_Agent = GetComponentInParent<Agent>();
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.CompareTag(m_GetupSwap))
{
// Swap Model to this
m_ModelSwapper.SwitchModel("Getup", m_Agent);
}
if (col.gameObject.CompareTag(m_WalkerSwap))
{
// Swap Model to this
m_ModelSwapper.SwitchModel("Walker", m_Agent);
}
if (col.gameObject.CompareTag(m_BalanceSwap))
{
// Swap Model to this
m_ModelSwapper.SwitchModel("Balance", m_Agent);
}
if (col.gameObject.CompareTag(m_SloperSwap))
{
// Swap Model to this
m_ModelSwapper.SwitchModel("Sloper", m_Agent);
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.CompareTag(m_GetupSwap))
{
m_ModelSwapper.SwitchModel(4, m_Agent);
}
}
}
}

11
Assets/7_Scripts/Model Swapping/SwapModelOnContact.cs.meta

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 5c07b818612782c47bc265efc0b072dd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/7_Scripts/Waypoint.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3ff613cd7a2113b478921209cc2a6bda
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

53
Assets/7_Scripts/Waypoint/Waypoint.cs

@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[ExecuteInEditMode]
public class Waypoint : MonoBehaviour
{
[HideInInspector]
public Transform m_WaypointPosition;
public float m_WaitTime;
public string m_ModelName;
public float m_Index;
public bool m_Touched;
public delegate void WaypointReached(float index);
public static event WaypointReached OnWaypointReached;
public delegate void SwapModelOnReachingWaypoint(string modelName);
public static event SwapModelOnReachingWaypoint SwapModelOnWaypointReached;
private void OnEnable()
{
m_WaypointPosition = this.transform;
}
void OnTriggerEnter(Collider other)
{
if (other.transform.CompareTag("agent") && !m_Touched)
{
if (OnWaypointReached != null)
{
m_Touched = true;
OnWaypointReached(m_Index);
if (!string.IsNullOrEmpty(m_ModelName))
{
SwapModelOnWaypointReached(m_ModelName);
}
}
}
}
}

2
Assets/7_Scripts/Waypoint/Waypoint.cs.meta

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a5c81c2052c91a74a8d9fae388fca038

109
Assets/7_Scripts/Waypoint/WaypointManager.cs

@ -0,0 +1,109 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[ExecuteInEditMode]
public class WaypointManager : MonoBehaviour
{
[SerializeField]
public List<Waypoint> m_WaypointList;
[SerializeField]
public Transform m_Target;
[SerializeField]
public Waypoint m_CurrentWaypoint;
private void OnEnable()
{
m_WaypointList = new List<Waypoint>();
Waypoint.OnWaypointReached += SetNextWaypoint;
}
private void OnDisable()
{
Waypoint.OnWaypointReached -= SetNextWaypoint;
}
private void Start()
{
SortWaypointsInChildren();
m_CurrentWaypoint = m_WaypointList[0];
MoveTargetToWaypoint();
}
private void FixedUpdate()
{
MoveTargetToWaypoint();
}
private void MoveTargetToWaypoint()
{
Vector3 wpPosition = m_CurrentWaypoint.m_WaypointPosition.position;
m_Target.position = wpPosition;
}
private void SetNextWaypoint(float index)
{
if (index == m_CurrentWaypoint.m_Index)
{
Debug.Log($"Waypoint reached {index}");
MoveToNextWaypoint(index);
}
else
{
Debug.Log($"Wrong Waypoint reached {index}");
}
}
private void MoveToNextWaypoint(float index)
{
StartCoroutine(WaypointTimeout(m_CurrentWaypoint.m_WaitTime, index));
}
IEnumerator WaypointTimeout(float seconds, float index)
{
yield return new WaitForSeconds(seconds);
if (m_CurrentWaypoint.m_Index < m_WaypointList.Count - 1)
{
m_CurrentWaypoint = m_WaypointList[(int)index + 1];
MoveTargetToWaypoint();
//Debug.Log($"Next waypoint {m_CurrentWaypoint.m_Index}");
}
else
{
Debug.Log($"Reached Last Position {m_CurrentWaypoint.m_Index}");
}
}
private void SortWaypointsInChildren()
{
foreach (Transform t in transform)
{
m_WaypointList.Add(t.GetComponent<Waypoint>());
}
m_WaypointList = m_WaypointList.OrderBy(waypoint => waypoint.m_Index).ToList();
}
void OnDrawGizmos()
{
#if UNITY_EDITOR
Gizmos.color = Color.red;
foreach (Waypoint waypoint in m_WaypointList)
{
Gizmos.DrawWireSphere(waypoint.m_WaypointPosition.position, 1f);
}
Gizmos.color = Color.red;
for (int i = 0; i < m_WaypointList.Count - 1; i++)
{
Gizmos.DrawLine(m_WaypointList[i].m_WaypointPosition.position, m_WaypointList[i + 1].m_WaypointPosition.position);
}
#endif
}
}

2
Assets/7_Scripts/Waypoint/WaypointManager.cs.meta

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1f83b258daac8e846bed96ffebb253e3

2
Assets/ML-Agents/Timers/Beep_timers.json

@ -1 +1 @@
{"count":1,"self":20.310344,"total":20.5002641,"children":{"InitializeActuators":{"count":1,"self":0.0020023,"total":0.0020023,"children":null},"InitializeSensors":{"count":1,"self":0.0010042,"total":0.0010042,"children":null},"AgentSendState":{"count":736,"self":0.0115652,"total":0.0332494,"children":{"CollectObservations":{"count":148,"self":0.0050284,"total":0.0050284,"children":null},"WriteActionMask":{"count":148,"self":0,"total":0,"children":null},"RequestDecision":{"count":148,"self":0.0166558,"total":0.0166558,"children":null}}},"DecideAction":{"count":736,"self":0.1456081,"total":0.1456081,"children":{"RayPerceptionSensor.Perceive":{"count":148,"self":0,"total":0,"children":null}}},"AgentAct":{"count":736,"self":0.0080564,"total":0.0080564,"children":null}},"gauges":{"c_Walker.CumulativeReward":{"count":2,"max":31.4808464,"min":29.9941273,"runningAverage":30.7374878,"value":31.4808464,"weightedAverage":30.3658066}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1708955771","unity_version":"2023.2.8f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.2.8f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_02-Beep\\4_Unity\\Beep Final -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Beep","end_time_seconds":"1708955792"}}
{"count":1,"self":18.9057456,"total":19.3335361,"children":{"InitializeActuators":{"count":1,"self":0.001511,"total":0.001511,"children":null},"InitializeSensors":{"count":1,"self":0.0020118,"total":0.0020118,"children":null},"AgentSendState":{"count":667,"self":0.0075721,"total":0.2773386,"children":{"CollectObservations":{"count":134,"self":0.0045404,"total":0.0045404,"children":null},"WriteActionMask":{"count":134,"self":0,"total":0,"children":null},"RequestDecision":{"count":134,"self":0.26522609999999996,"total":0.26522609999999996,"children":null}}},"DecideAction":{"count":667,"self":0.1403981,"total":0.1403981,"children":{"RayPerceptionSensor.Perceive":{"count":134,"self":0,"total":0,"children":null}}},"AgentAct":{"count":667,"self":0.0065305,"total":0.0065305,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1708957870","unity_version":"2023.2.8f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.2.8f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_02-Beep\\4_Unity\\Beep Final -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Beep","end_time_seconds":"1708957889"}}
Loading…
Cancel
Save