浏览代码

update reward func & add friendly fire

/develop/dodgeball-tests
HH 4 年前
当前提交
49c46ac5
共有 5 个文件被更改,包括 155 次插入50 次删除
  1. 121
      Project/Assets/ML-Agents/Examples/FPS_Game/Scenes/FPS_Game.unity
  2. 18
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBall.cs
  3. 48
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallAgent.cs
  4. 13
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallGameController.cs
  5. 5
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ThrowBall.cs

121
Project/Assets/ML-Agents/Examples/FPS_Game/Scenes/FPS_Game.unity


propertyPath: m_TagString
value: purpleAgent
objectReference: {fileID: 0}
- target: {fileID: 8366924173548761677, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: m_LocalPosition.z
value: 0.85
objectReference: {fileID: 0}
- target: {fileID: 8367363780074625334, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: m_IsActive

- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}
propertyPath: m_LocalPosition.x
value: 16.7
value: 20.171
value: 1.111
value: 1.5438008
value: -4.63
value: -1.801837
value: 0
value: -0
value: 0
value: -0
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}

- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}
propertyPath: m_RootOrder
value: 6
value: 12
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}

type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}
propertyPath: m_LocalScale.x
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}
propertyPath: m_LocalScale.z
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854908, guid: 4c3111f00dcbe425186833fe890fcac7,
type: 3}

type: 3}
m_PrefabInstance: {fileID: 860687286}
m_PrefabAsset: {fileID: 0}
--- !u!1 &861736839
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 861736840}
m_Layer: 10
m_Name: SpawnPos (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!4 &861736840
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 861736839}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.42, y: 0.402, z: 0.85}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1234972960}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &872039382 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 2170513384791859414, guid: 497c5a1fa56ff4204a66acfb42a8ebde,

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1217689138}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1234972960 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 8366924173413481312, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
m_PrefabInstance: {fileID: 8366924174629554752}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1236036022
GameObject:
m_ObjectHideFlags: 0

- target: {fileID: 2170513384791859414, guid: 497c5a1fa56ff4204a66acfb42a8ebde,
type: 3}
propertyPath: m_RootOrder
value: 9
value: 8
objectReference: {fileID: 0}
- target: {fileID: 2170513384791859414, guid: 497c5a1fa56ff4204a66acfb42a8ebde,
type: 3}

NumberOfTimesPlayerCanBeHit: 5
HitPointsRemaining: 0
m_Initialized: 0
m_BehaviorParameters: {fileID: 0}
m_InputH: 0
leftStrafe: 0
rightStrafe: 0

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 11
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2095421680
MonoBehaviour:

m_LocalScale: {x: 1, y: 1.0000002, z: 1.0000002}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 10
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: -90, y: 0, z: 0}
--- !u!114 &5681435198854464166
MonoBehaviour:

- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}
propertyPath: m_LocalPosition.x
value: 15.86
value: 20.171
value: 1.729
value: 1.5428826
value: -6.29
value: -2.2649236
value: 0
value: -0
value: 0
value: -0
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}

- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}
propertyPath: m_RootOrder
value: 8
value: 6
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}

type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}
propertyPath: m_LocalScale.x
value: 0.255744
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854904, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}
propertyPath: m_LocalScale.z
value: 0.255744
objectReference: {fileID: 0}
- target: {fileID: 3911029810215854908, guid: 01e75f85d5a3c451b9a5bed7070be0a0,
type: 3}

propertyPath: Cam
value:
objectReference: {fileID: 8697938321410268831}
- target: {fileID: 8366924173548761677, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8366924173548761677, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8366924173548761677, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8600068265116176913, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}
propertyPath: UISlider

type: 3}
propertyPath: m_LocalRotation.x
value: 0.023514416
value: 0.023514433
value: 3.1894046e-10
value: -0.0000000052765476
value: -7.5017536e-12
value: 1.2410933e-10
objectReference: {fileID: 0}
- target: {fileID: 9116796388679672959, guid: 1c6bcdaf8b76745d8918fc47c464885e,
type: 3}

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8697938321410268827}
m_LocalRotation: {x: -7.5017536e-12, y: 0.9997235, z: -0.023514416, w: -3.1894046e-10}
m_LocalRotation: {x: 1.2410933e-10, y: 0.9997235, z: -0.023514433, w: 0.0000000052765476}
m_RootOrder: 12
m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8697938321410268829
MonoBehaviour:

18
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBall.cs


public Rigidbody rb;
public Collider BallCollider;
public int TeamToIgnore;
// Start is called before the first frame update
void Start()
{

//Set ball to either a pickup item or an active ball that is in play
//inPlay = true means the ball can hurt other players
public void BallIsInPlay(bool p)
//ignoreTeam = the TeamID to ignore. *friendly fire mechanic
public void BallIsInPlay(bool p, int ignoreTeam = -1)
gameObject.tag = "dodgeBallActive";
BallCollider.gameObject.tag = "dodgeBallActive";
TagBallAs( "dodgeBallActive");
gameObject.tag = "dodgeBallPickup";
BallCollider.gameObject.tag = "dodgeBallPickup";
TagBallAs( "dodgeBallPickup");
TeamToIgnore = ignoreTeam;
}
void TagBallAs(string tag)
{
gameObject.tag = tag;
BallCollider.gameObject.tag = tag;
private void OnCollisionEnter(Collision col)
{
//IF NOT MY TEAM

48
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallAgent.cs


using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Policies;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;

public int teamID;
private AgentCubeMovement m_CubeMovement;
public ThrowBall ThrowController;

public int HitPointsRemaining; //how many more times can we be hit
public bool m_Initialized;
[HideInInspector]
public BehaviorParameters m_BehaviorParameters;
//PLAYER STATE TO OBSERVE

m_CubeMovement = GetComponent<AgentCubeMovement>();
m_BehaviorParameters = gameObject.GetComponent<BehaviorParameters>();
// m_Cam = Camera.main;
m_AgentRb = GetComponent<Rigidbody>();
input = GetComponent<FPSAgentInput>();

{
if (currentNumberOfBalls > 0)
{
ThrowController.Throw(ActiveBallsQueue.Peek());
var db = ActiveBallsQueue.Peek();
ThrowController.Throw(db, m_BehaviorParameters.TeamId);
ActiveBallsQueue.Dequeue();
currentNumberOfBalls--;
SetActiveBalls(currentNumberOfBalls);

if (db.inPlay) //HIT BY LIVE BALL
{
m_GameController.PlayerWasHit(this);
// if (HitPointsRemaining == 1)
// {
// //RESET ENV
// print($"{gameObject.name} Lost.{gameObject.name} was weak:");
// //ASSIGN REWARDS
// EndEpisode();
// }
// else
// {
// HitPointsRemaining--;
// //ASSIGN REWARDS
//
// }
print("HIT BY LIVE BALL");
// if(HitByParticles.isPlaying)
db.BallIsInPlay(false);
if (db.TeamToIgnore != -1 && db.TeamToIgnore != m_BehaviorParameters.TeamId) //HIT BY LIVE BALL
{
m_GameController.PlayerWasHit(this);
// if (HitPointsRemaining == 1)
// {
// //RESET ENV
// print($"{gameObject.name} Lost.{gameObject.name} was weak:");
// //ASSIGN REWARDS
// EndEpisode();
// }
// else
// {
// HitPointsRemaining--;
// //ASSIGN REWARDS
//
// }
print("HIT BY LIVE BALL");
// if(HitByParticles.isPlaying)
db.BallIsInPlay(false);
}
}
else //TRY TO PICK IT UP
{

13
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallGameController.cs


{
item.Agent.Initialize();
item.HitPointsRemaining = PlayerMaxHitPoints;
item.Agent.m_BehaviorParameters.TeamId = 0;
item.TeamID = 0;
PlayersDict.Add(item.Agent, item);
}

item.HitPointsRemaining = PlayerMaxHitPoints;
item.Agent.m_BehaviorParameters.TeamId = 1;
item.TeamID = 1;
PlayersDict.Add(item.Agent, item);
}

public void PlayerWasHit(DodgeBallAgent agent)
{
// var team
//SET AGENT/TEAM REWARDS HERE
int hitTeamID = info.TeamID;
var HitTeamList = hitTeamID == 0 ? Team0Players : Team1Players;
var HitByTeamList = hitTeamID == 1 ? Team0Players : Team1Players;
// int hitByTeamID = hitTeamID == 0? 1: 0; //assumes only 2 teams
if (info.HitPointsRemaining == 1)
{
//RESET ENV

agent.AddReward(-1f); //you lost penalty
HitByTeamList[0].Agent.AddReward(1);
if (info.TeamID == 0)
{
print($"Team 1 Won");

info.HitPointsRemaining--;
//ASSIGN REWARDS
agent.AddReward(-.1f); //small hit penalty
HitByTeamList[0].Agent.AddReward(.1f);
}

5
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/ThrowBall.cs


// }
// }
public void Throw(DodgeBall db)
//ignoreTeam. 0 ignores team 0, 1 ignores team 1, -1 ignores no teams
public void Throw(DodgeBall db, int ignoreTeam = -1)
db.BallIsInPlay(true);
db.BallIsInPlay(true, ignoreTeam);
FireProjectile(db.rb);
}

正在加载...
取消
保存