分离所有RewardFunction相关到单独的一个Script里。
This commit is contained in:
@@ -5,22 +5,8 @@ using UnityEngine;
|
||||
|
||||
public class AgentController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject commonParameterContainerObj;
|
||||
[SerializeField]
|
||||
private GameObject environmentObj;
|
||||
[SerializeField]
|
||||
private GameObject enemyContainerObj;
|
||||
[SerializeField]
|
||||
private GameObject sceneBlockContainerObj;
|
||||
[SerializeField]
|
||||
private GameObject environmentUIControlObj;
|
||||
[SerializeField]
|
||||
private GameObject targetControllerObj;
|
||||
[SerializeField]
|
||||
private GameObject HUDObj;
|
||||
[SerializeField]
|
||||
private Camera fpsCam;
|
||||
[SerializeField] public GameObject commonParameterContainerObj;
|
||||
[SerializeField] public Camera fpsCam;
|
||||
|
||||
[Header("GetAxis() Simulate")]
|
||||
public float moveSpeed = 9.0f;
|
||||
@@ -33,45 +19,33 @@ public class AgentController : MonoBehaviour
|
||||
public float mouseYSensitivity = 200;
|
||||
public float yRotation = 0.1f;//定义一个浮点类型的量,记录‘围绕’X轴旋转的角度
|
||||
|
||||
private List<float> spinRecord = new List<float>();
|
||||
private bool lockMouse;
|
||||
private float damage;
|
||||
private float fireRate;
|
||||
private bool lockCameraX;
|
||||
private bool lockCameraY;
|
||||
|
||||
// environment
|
||||
private float lastShootTime = 0.0f;
|
||||
public float lastShootTime = 0.0f;
|
||||
|
||||
private int enemyKillCount = 0;
|
||||
private Vector3 killEnemyPosition;
|
||||
public int enemyKillCount = 0;
|
||||
public Vector3 killEnemyPosition;
|
||||
public bool defaultTPCamera = true;
|
||||
|
||||
[System.NonSerialized] public bool gunReadyToggle = true;
|
||||
private string myTag = "";
|
||||
private float lastEnemyFacingDistance = 0f; // record last enemy facing minimum distance
|
||||
private float lastTargetFacingDistance = 0f; // record last target facing minimum distance
|
||||
public string myTag = "";
|
||||
|
||||
// scripts
|
||||
private RaySensors raySensors;
|
||||
|
||||
private CharacterController playerController;
|
||||
private CommonParameterContainer commonPramCon;
|
||||
private SceneBlockContainer blockContainer;
|
||||
private TargetController targetCon;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// initialize scripts
|
||||
commonPramCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
||||
blockContainer = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
||||
targetCon = targetControllerObj.GetComponent<TargetController>();
|
||||
raySensors = GetComponent<RaySensors>();
|
||||
playerController = this.transform.GetComponent<CharacterController>();
|
||||
playerController = transform.GetComponent<CharacterController>();
|
||||
|
||||
// initialize Environment parameters
|
||||
lockMouse = commonPramCon.lockMouse;
|
||||
damage = commonPramCon.damage;
|
||||
fireRate = commonPramCon.fireRate;
|
||||
lockCameraX = commonPramCon.lockCameraX;
|
||||
lockCameraY = commonPramCon.lockCameraY;
|
||||
@@ -199,234 +173,7 @@ public class AgentController : MonoBehaviour
|
||||
|
||||
#endregion Camera Control
|
||||
|
||||
#region Reward Functions
|
||||
|
||||
// ballistic 射击弹道处理,并返回获得reward
|
||||
private float Ballistic(int shootState)
|
||||
{
|
||||
Vector3 point = new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0);//发射位置
|
||||
Ray ray = fpsCam.ScreenPointToRay(point);
|
||||
RaycastHit hit;
|
||||
// Debug.DrawRay(centerRay.origin, centerRay.direction * 100, Color.blue);
|
||||
//按下鼠标左键
|
||||
if (shootState != 0 && gunReadyToggle == true)
|
||||
{
|
||||
lastShootTime = Time.time;
|
||||
if (Physics.Raycast(ray, out hit, 100))
|
||||
{
|
||||
if (hit.collider.tag != myTag && hit.collider.tag != "Wall" && hit.collider.tag != "Untagged")
|
||||
{
|
||||
// kill enemy
|
||||
GameObject gotHitObj = hit.transform.gameObject;//获取受到Ray撞击的对象
|
||||
gotHitObj.GetComponent<States>().ReactToHit(damage, gameObject);
|
||||
shootState = 0;
|
||||
return targetCon.HitEnemyReward(gotHitObj.transform.position);
|
||||
}
|
||||
}
|
||||
if (targetCon.targetTypeInt == (int)Targets.Attack)
|
||||
{
|
||||
// while if attack mode
|
||||
float targetDis = Vector3.Distance(blockContainer.nowBlock.transform.position, transform.position);
|
||||
if (targetDis <= raySensors.viewDistance)
|
||||
{
|
||||
// Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red);
|
||||
if (Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.nowBlock.transform.position) <= blockContainer.nowBlock.firebasesAreaDiameter / 2)
|
||||
{
|
||||
// im shooting at target but didn't hit enemy
|
||||
// Debug.DrawRay(centerRay.origin, viewPoint-centerRay.origin, Color.blue);
|
||||
return commonPramCon.shootTargetAreaReward;
|
||||
}
|
||||
}
|
||||
}
|
||||
shootState = 0;
|
||||
return commonPramCon.shootReward;
|
||||
}
|
||||
else if (shootState != 0 && gunReadyToggle == false)
|
||||
{
|
||||
// shoot without ready
|
||||
shootState = 0;
|
||||
return commonPramCon.shootWithoutReadyReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
// do not shoot
|
||||
shootState = 0;
|
||||
return commonPramCon.nonReward;
|
||||
}
|
||||
}
|
||||
|
||||
private float FacingReward()
|
||||
{
|
||||
float nowReward = 0;
|
||||
bool isFacingtoEnemy = false;
|
||||
float enemyFacingDistance = 0f;
|
||||
|
||||
Vector3 screenCenter = new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0);
|
||||
Vector3 screenLeft = new Vector3(0, fpsCam.pixelHeight / 2, 0);
|
||||
|
||||
Ray centerRay = fpsCam.ScreenPointToRay(screenCenter);
|
||||
Ray leftRay = fpsCam.ScreenPointToRay(screenLeft);
|
||||
|
||||
// target fireBaseArea Position, turen y to camera's y
|
||||
Vector3 fireBaseArea = blockContainer.nowBlock.fireBasesAreaObj.transform.position;
|
||||
fireBaseArea.y = fpsCam.transform.position.y;
|
||||
|
||||
// my position, turn y to camera's y
|
||||
// Debug.DrawRay(fpsCam.transform.position, centerRay.direction * 100, Color.blue);
|
||||
Vector3 myposition = transform.position;
|
||||
myposition.y = fpsCam.transform.position.y;
|
||||
|
||||
// Target to Agent distance
|
||||
//Debug.DrawLine(fireBaseArea, myposition, Color.red);
|
||||
float targetDis = Vector3.Distance(fireBaseArea, myposition);
|
||||
|
||||
// point in centerRay and leftRay which distance is targetDis from camera center
|
||||
Vector3 pointInCenterRay = fpsCam.transform.position + (centerRay.direction * targetDis);
|
||||
Vector3 pointInLeftRay = fpsCam.transform.position + (leftRay.direction * targetDis);
|
||||
|
||||
// center of screen to target's distance
|
||||
// Debug.DrawLine(pointInCenterRay, fireBaseArea,Color.green);
|
||||
float camCenterToTarget = Vector3.Distance(pointInCenterRay, fireBaseArea);
|
||||
|
||||
// left of screen to target's distance
|
||||
// Debug.DrawLine(pointInLeftRay, pointInCenterRay, Color.yellow);
|
||||
float camCenterToViewEdge = Vector3.Distance(pointInLeftRay, pointInCenterRay);
|
||||
|
||||
switch (targetCon.targetTypeInt)
|
||||
{
|
||||
case (int)Targets.Free:
|
||||
//free mode
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(centerRay, out hit, 100))
|
||||
{
|
||||
// facing to an enemy
|
||||
if (hit.collider.tag != myTag && hit.collider.tag != "Wall")
|
||||
{
|
||||
nowReward = commonPramCon.facingReward;
|
||||
isFacingtoEnemy = true;
|
||||
}
|
||||
}
|
||||
if (raySensors.inViewEnemies.Count > 0 && !isFacingtoEnemy)
|
||||
{
|
||||
// have enemy in view
|
||||
List<float> projectionDis = new List<float>();
|
||||
foreach (GameObject theEnemy in raySensors.inViewEnemies)
|
||||
{
|
||||
// for each enemy in view
|
||||
Vector3 projection = Vector3.Project(theEnemy.transform.position - transform.position, (centerRay.direction * 10));
|
||||
Vector3 verticalToRay = transform.position + projection - theEnemy.transform.position;
|
||||
projectionDis.Add(verticalToRay.magnitude);
|
||||
// Debug.Log("enemy!" + verticalToRay.magnitude);
|
||||
// Debug.DrawRay(transform.position, (centerRay.direction * 100), Color.cyan);
|
||||
// Debug.DrawRay(transform.position, theEnemy.transform.position - transform.position, Color.yellow);
|
||||
// Debug.DrawRay(transform.position, projection, Color.blue);
|
||||
// Debug.DrawRay(theEnemy.transform.position, verticalToRay, Color.magenta);
|
||||
}
|
||||
enemyFacingDistance = projectionDis.Min();
|
||||
if (enemyFacingDistance <= lastEnemyFacingDistance)
|
||||
{
|
||||
// closing to enemy
|
||||
nowReward = 1 / MathF.Sqrt(commonPramCon.facingInviewEnemyDisCOEF * enemyFacingDistance + 0.00001f);
|
||||
}
|
||||
else
|
||||
{
|
||||
nowReward = 0;
|
||||
}
|
||||
// enemy in view Reward
|
||||
lastEnemyFacingDistance = enemyFacingDistance;
|
||||
if (nowReward >= commonPramCon.facingReward) nowReward = commonPramCon.facingReward; // limit
|
||||
if (nowReward <= -commonPramCon.facingReward) nowReward = -commonPramCon.facingReward; // limit
|
||||
// Debug.Log("ninimum = " + nowReward);
|
||||
}
|
||||
break;
|
||||
case (int)Targets.Attack:
|
||||
// attack mode
|
||||
if (targetDis <= raySensors.viewDistance)
|
||||
{
|
||||
// Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red);
|
||||
// while center of screen between target's distance is lower than firebasesAreaDiameter
|
||||
// while facing to target
|
||||
if (camCenterToTarget <= blockContainer.nowBlock.firebasesAreaDiameter / 2)
|
||||
{
|
||||
// Debug.DrawRay(centerRay.origin, viewPoint-centerRay.origin, Color.blue);
|
||||
nowReward = commonPramCon.facingReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
// while not facing to target
|
||||
nowReward = (lastTargetFacingDistance - camCenterToTarget) * commonPramCon.facingTargetReward;
|
||||
}
|
||||
}
|
||||
// update lastTargetFacingDistance
|
||||
lastTargetFacingDistance = camCenterToTarget;
|
||||
break;
|
||||
case (int)Targets.Go:
|
||||
// goto mode
|
||||
if (camCenterToTarget <= camCenterToViewEdge)
|
||||
{
|
||||
// fireArea is in view
|
||||
nowReward = commonPramCon.facingReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
nowReward = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Debug.LogError("Wrong target type");
|
||||
break;
|
||||
}
|
||||
return nowReward;
|
||||
}
|
||||
|
||||
public float RewardCalculate(float sceneReward, float mouseX, float movement, int shootState)
|
||||
{
|
||||
float epreward = 0f;
|
||||
// 击杀reward判断
|
||||
if (enemyKillCount > 0)
|
||||
{
|
||||
for (int i = 0; i < enemyKillCount; i++)
|
||||
{
|
||||
// get
|
||||
epreward += targetCon.KillReward(killEnemyPosition);
|
||||
}
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
// 射击动作reward判断
|
||||
epreward += Ballistic(shootState) + sceneReward;
|
||||
// facing reward
|
||||
epreward += FacingReward();
|
||||
// Penalty
|
||||
// spin penalty
|
||||
spinRecord.Add(mouseX);
|
||||
if (spinRecord.Count >= commonPramCon.spinRecordMax)
|
||||
{
|
||||
spinRecord.RemoveAt(0);
|
||||
}
|
||||
float spinPenaltyReward = Math.Abs(spinRecord.ToArray().Sum() * commonPramCon.spinPenalty);
|
||||
if (spinPenaltyReward >= commonPramCon.spinPenaltyThreshold)
|
||||
{
|
||||
epreward -= spinPenaltyReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
epreward -= Math.Abs(mouseX) * commonPramCon.mousePenalty;
|
||||
}
|
||||
// move penalty
|
||||
if (movement != 0)
|
||||
{
|
||||
epreward -= commonPramCon.movePenalty;
|
||||
}
|
||||
return epreward;
|
||||
}
|
||||
|
||||
#endregion Reward Functions
|
||||
|
||||
// GotKill 获得击杀时用于被呼出
|
||||
// Got Kill point
|
||||
public void KillRecord(Vector3 killEnemyPosition)
|
||||
{
|
||||
enemyKillCount += 1;
|
||||
|
||||
Reference in New Issue
Block a user