Added respawn for AI, Game manager keeps track of laps
This commit is contained in:
@@ -33,6 +33,8 @@ public class AgentController: Agent
|
||||
public int distanceBetweenCheckpoints = 5;
|
||||
public bool ignoreMentalPain = true;
|
||||
bool isEnabled = true;
|
||||
public bool isPlaying = false;
|
||||
[HideInInspector] public bool isFinished = false; // needed for gamemanager
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
@@ -56,9 +58,13 @@ public class AgentController: Agent
|
||||
return;
|
||||
|
||||
stepsSinceCheckpoint = 0;
|
||||
checkpointsCollected = 0;
|
||||
totalReward = 0;
|
||||
totalMentalPain = 0;
|
||||
checkpointsCollected = 0;
|
||||
|
||||
// don't reset car unless in training
|
||||
if (isPlaying)
|
||||
return;
|
||||
|
||||
// reset wheels
|
||||
foreach (var wheel in wheels)
|
||||
@@ -92,6 +98,12 @@ public class AgentController: Agent
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
if (!isEnabled)
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
sensor.AddObservation(0);
|
||||
return;
|
||||
}
|
||||
|
||||
Transform currentCheckpoint = checkpoints[checkpointsCollected].transform;
|
||||
|
||||
@@ -249,7 +261,16 @@ public class AgentController: Agent
|
||||
if (checkpointsCollected == checkpoints.Count - 1)
|
||||
{
|
||||
AddReward(10f);
|
||||
EndEpisode();
|
||||
|
||||
if (isPlaying)
|
||||
{
|
||||
isFinished = true;
|
||||
}
|
||||
|
||||
EndEpisode();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
checkpointsCollected += 1;
|
||||
@@ -268,7 +289,34 @@ public class AgentController: Agent
|
||||
if (stepsSinceCheckpoint >= maxStepsPerCheckpoint)
|
||||
{
|
||||
stepsSinceCheckpoint = 0;
|
||||
EndEpisode();
|
||||
|
||||
if (isPlaying) // send back to previous checkpoint if stuck
|
||||
{
|
||||
|
||||
if (checkpointsCollected == 0)
|
||||
{
|
||||
transform.position = startPosition;
|
||||
transform.rotation = startRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.position = new Vector3(
|
||||
checkpoints[checkpointsCollected - 1].transform.position.x,
|
||||
transform.position.y + 3,
|
||||
checkpoints[checkpointsCollected - 1].transform.position.z
|
||||
);
|
||||
|
||||
transform.eulerAngles = new Vector3(
|
||||
transform.eulerAngles.x,
|
||||
checkpoints[checkpointsCollected - 1].transform.eulerAngles.y,
|
||||
transform.eulerAngles.z
|
||||
);
|
||||
}
|
||||
|
||||
rb.velocity = Vector3.zero;
|
||||
}
|
||||
else
|
||||
EndEpisode();
|
||||
}
|
||||
|
||||
// print(GetCumulativeReward());
|
||||
|
||||
@@ -15,8 +15,17 @@ public class CarLoader : MonoBehaviour
|
||||
public GameObject targetcar;
|
||||
void Start()
|
||||
{
|
||||
carType = GameObject.Find("Car Data").GetComponent<CarData>().carType;
|
||||
carColor = GameObject.Find("Car Data").GetComponent<CarData>().carColor;
|
||||
try
|
||||
{
|
||||
carType = GameObject.Find("Car Data").GetComponent<CarData>().carType;
|
||||
carColor = GameObject.Find("Car Data").GetComponent<CarData>().carColor;
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
carType = "Racecar";
|
||||
carColor = "Default";
|
||||
print("no loading");
|
||||
}
|
||||
|
||||
foreach (GameObject car in Cars)
|
||||
{
|
||||
|
||||
@@ -24,16 +24,40 @@ public class GameManager : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
foreach (GameObject player in players)
|
||||
for (int i = 0; i < players.Count(); i++)
|
||||
{
|
||||
try
|
||||
GameObject player = players[i];
|
||||
|
||||
bool isAI = true;
|
||||
if (player.GetComponent<PlayerController>().enabled)
|
||||
isAI = false;
|
||||
|
||||
if (isAI)
|
||||
{
|
||||
bool isFinished = player.GetComponent<AgentController>().isFinished;
|
||||
|
||||
if (isFinished)
|
||||
{
|
||||
player.GetComponent<AgentController>().isFinished = false;
|
||||
playerLaps[i] += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int checkpointsCollected = player.GetComponent<PlayerController>().checkpointsCollected;
|
||||
|
||||
if (checkpointsCollected == checkpoints.Count())
|
||||
{
|
||||
player.GetComponent<PlayerController>().checkpointsCollected = 0;
|
||||
playerLaps[i] += 1;
|
||||
}
|
||||
}
|
||||
catch
|
||||
|
||||
if (playerLaps[i] > lapAmount)
|
||||
{
|
||||
int checkpointsCollected = player.GetComponent<AgentController>().checkpointsCollected;
|
||||
player.GetComponent<Rigidbody>().isKinematic = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user