Can you see anything wrong with the following code?
public override void CollectObservations(VectorSensor sensor)
{
Vector3 agentPosition = me.transform.localPosition;
// Vector3 missilePosition = missileObject.transform.position;
// Vector3 missileVelocity = missileObject.GetComponent().velocity;
Vector3 center = dome.transform.localPosition;
// float radius = dome.transform.localScale.x / 1.5f; // Assuming a uniform scale
float radius = dome.transform.localScale.x / 2; // Assuming a uniform scale
Vector3 objectToPlayer = me.transform.localPosition - missile.transform.localPosition;
Vector3 objectVelocity = missile.GetComponent().velocity;
playerTransform = me.transform;
if (Missiles.Length > 0)
{
foreach (GameObject missileObject in Missiles)
{
missileTransform = missileObject.transform;
Vector3 missileToPlayerLocal = playerTransform.localPosition - missileTransform.localPosition;
Vector3 missileToPlayerWorld = missileTransform.TransformDirection(missileToPlayerLocal);
float dotProduct = Vector3.Dot(missileToPlayerWorld.normalized, missileTransform.forward);
towards = dotProduct;
// float dotProduct = Vector3.Dot(missileToPlayerWorld.normalized, missileTransform.forward);
// towards = Vector3.Dot(objectToPlayer.normalized, objectVelocity.normalized);
Vector3 missileDirection = missileObject.transform.forward;
angle = Vector3.Angle(missileToPlayerLocal, missileDirection);
missileObject.GetComponent().AOA = angle;
DOT = angle;
Vector3 centr = dome.transform.position;
radius1 = dome.transform.localScale.x * 4; // Assuming a uniform scale
// Calculate the distance between the point and the center of the sphere
float distance = Vector3.Distance(missile.transform.localPosition, centr);
if (distance <= radius1)
{
close = true;
}
else
{
close = false;
}
missileObject.GetComponent().close1 = close;
sensor.AddObservation(shouldFire);
sensor.AddObservation(missileObject.transform.localPosition);
sensor.AddObservation(me.transform.localPosition);
sensor.AddObservation(missileObject.GetComponent().AOA);
sensor.AddObservation(missileObject.GetComponent().close1);
}
}
public override void OnActionReceived(ActionBuffers actions)
{
// base.OnActionReceived(actions);
actions_count++;
for (int i = 0; i < Missiles.Length; i++)
{
// RequestDecision();
//bool shouldFire = actions.DiscreteActions.Array[0] == 1;
shouldFire = actions.DiscreteActions[0];
GameObject missileObject = Missiles*;*
if (missileObject.transform.localPosition.y > 1 && missileObject.tag == “Missile”)
{
// Each Missle’s Angle Calc
missileTransform = missileObject.transform;
Vector3 missileToPlayerLocal = playerTransform.localPosition - missileTransform.localPosition;
Vector3 missileDirection = missileObject.transform.forward;
angle = Vector3.Angle(missileToPlayerLocal, missileDirection);
missileObject.GetComponent().AOA = angle;
//// Distance
//Vector3 centr = dome.transform.position;
//radius1 = dome.transform.localScale.x * 4; // Assuming a uniform scale
//float distance = Vector3.Distance(missile.transform.localPosition, centr);
//if (distance <= radius1)
//{
// close = true;
//}
// Fired as coming towards
if (shouldFire == 1 && missileObject.GetComponent().AOA < 90)
{
DrawLine(me.transform.localPosition, missileObject.transform.localPosition, Color.green, 0.5f);
// GameObject.Instantiate(Explos, missileObject.transform.localPosition, missileObject.transform.rotation);
// missile.tag = “landed”
SetReward(1);
corr_shot++;
// EndEpisode();
}
// Fire as moving away - wasted shot
if (shouldFire == 1 && missileObject.GetComponent().AOA > 90)
{
DrawLine(me.transform.localPosition, missileObject.transform.localPosition, Color.red, 0.5f);
SetReward(-1);
wasted_shot++;
// EndEpisode();
}
// Didn’t fire when should have !!
if (shouldFire == 0 && missileObject.GetComponent().AOA < 90)
{
// DrawLine(me.transform.localPosition, missile.transform.localPosition, Color.yellow, 0.5f);
SetReward(-1);
//Debug.Log("Fire = " + actions.DiscreteActions[0] + " Towards = " + towards);
Should_have_shot++;
// EndEpisode();
}
// Didn’t fire & Moving away - correct
if (shouldFire == 0 && missileObject.GetComponent().AOA > 90)
{
// DrawLine(me.transform.position, missile.transform.position, Color.green, 1.0f);
SetReward(1);
corr_left++;
// EndEpisode();
} //
if (shouldFire == 1 && missileObject.GetComponent().close1 == true)
{
DrawLine(me.transform.position, missile.transform.position, Color.yellow, 1.0f);
SetReward(1);
corr_shot++;
// EndEpisode();
} //
if (shouldFire == 1 && missileObject.GetComponent().close1 == false)
{
DrawLine(me.transform.position, missile.transform.position, Color.cyan, 1.0f);
SetReward(-1);
// wasted_shot++;
// EndEpisode();
} //
//if (missile.tag == “landed”)
//{
// EndEpisode();
//}
}
}