Game Manager WIP

This commit is contained in:
2024-04-24 08:53:46 +02:00
parent ccd2ea506b
commit 7b313fe2d4
14 changed files with 373 additions and 473 deletions

View File

@@ -11,7 +11,7 @@ using Unity.VisualScripting;
using System.Reflection;
using System;
public class AgentControllerV7 : Agent
public class AgentController: Agent
{
public float motorTorque = 300;
public float brakeTorque = 500;
@@ -32,8 +32,13 @@ public class AgentControllerV7 : Agent
public int maxStepsPerCheckpoint = 300;
public int distanceBetweenCheckpoints = 5;
public bool ignoreMentalPain = true;
bool isEnabled = true;
// Start is called before the first frame update
protected override void OnDisable()
{
isEnabled = false;
return;
}
void Start()
{
rb = GetComponent<Rigidbody>();
@@ -47,6 +52,9 @@ public class AgentControllerV7 : Agent
public override void OnEpisodeBegin()
{
if (!isEnabled)
return;
stepsSinceCheckpoint = 0;
checkpointsCollected = 0;
totalReward = 0;
@@ -84,6 +92,7 @@ public class AgentControllerV7 : Agent
public override void CollectObservations(VectorSensor sensor)
{
Transform currentCheckpoint = checkpoints[checkpointsCollected].transform;
// distance to next checkpoint
@@ -124,7 +133,9 @@ public class AgentControllerV7 : Agent
}
public override void OnActionReceived(ActionBuffers actions)
{
{
if (!isEnabled)
return;
// Actions size = 2 [vertical speed, horizontal speed] = [-1..1, -1..1] // discrete = [{0, 1, 2}, {0, 1, 2}] = [{-1, 0, 1}...]
float vInput = 0;
float hInput = 0;
@@ -265,6 +276,9 @@ public class AgentControllerV7 : Agent
public override void Heuristic(in ActionBuffers actionsOut)
{
if (!isEnabled)
return;
var discreteActionsOut = actionsOut.DiscreteActions;
discreteActionsOut[0] = 2;
@@ -298,6 +312,9 @@ public class AgentControllerV7 : Agent
}
private void OnCollisionEnter(Collision other) {
if (!isEnabled)
return;
// if (other.gameObject.tag == "NPC")
// {
// AddReward(0.1f);

View File

@@ -30,7 +30,7 @@ public class CarLoader : MonoBehaviour
void replaceCar(GameObject car, string color)
{
car.GetComponent<PlayerController>().enabled = true;
car.GetComponent<AgentControllerV6>().enabled = false;
car.GetComponent<AgentController>().enabled = false;
cam.GetComponent<CameraControl>().getCar(car);
//LookAt = car.transform;

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class GameManager : MonoBehaviour
@@ -8,10 +9,16 @@ public class GameManager : MonoBehaviour
public GameObject[] checkpoints;
public GameObject[] players;
int currentLap = 1;
int[] playerLaps;
void Start()
{
// reset laps
playerLaps = new int[players.Count()];
for (int i = 0; i < playerLaps.Count(); i++)
{
playerLaps[i] = 1;
}
}
// Update is called once per frame
@@ -19,7 +26,14 @@ public class GameManager : MonoBehaviour
{
foreach (GameObject player in players)
{
try
{
int checkpointsCollected = player.GetComponent<PlayerController>().checkpointsCollected;
}
catch
{
int checkpointsCollected = player.GetComponent<AgentController>().checkpointsCollected;
}
}
}
}

View File

@@ -121,21 +121,27 @@ public class PlayerController : MonoBehaviour
}
private void Update() {
Transform currentCheckpoint = checkpoints[checkpointsCollected].transform;
float checkpintDistance = distanceToCheckpoint(currentCheckpoint);
if (checkpintDistance < 0.1f)
{
checkpointsCollected += 1;
}
}
private void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "Wall")
{
// audio.Play();
}
}
private void OnTriggerEnter(Collider other) {
print(checkpointsCollected);
Transform currentCheckpoint = checkpoints[checkpointsCollected].transform;
if (other.gameObject == currentCheckpoint)
{
checkpointsCollected += 1;
print(checkpointsCollected);
}
float distanceToCheckpoint(Transform checkpoint)
{
var closestPoint = checkpoint.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
var distanceToCheckpoint = Vector3.Distance(transform.position, closestPoint);
return distanceToCheckpoint;
}
}

View File

@@ -11,7 +11,7 @@ using Unity.VisualScripting;
using System.Reflection;
using System;
public class AgentController : Agent
public class AgentControllerOld : Agent
{
public float motorTorque = 300;
public float brakeTorque = 500;

View File

@@ -32,6 +32,7 @@ public class AgentControllerV6 : Agent
public int maxStepsPerCheckpoint = 300;
public int distanceBetweenCheckpoints = 5;
public bool ignoreMentalPain = false;
bool isEnabled = true;
void Awake()
{
@@ -45,11 +46,15 @@ public class AgentControllerV6 : Agent
protected override void OnDisable()
{
isEnabled = false;
return;
}
public override void OnEpisodeBegin()
{
if (!isEnabled)
return;
stepsSinceCheckpoint = 0;
checkpointsReached = 0;
totalReward = 0;
@@ -130,6 +135,9 @@ public class AgentControllerV6 : Agent
public override void OnActionReceived(ActionBuffers actions)
{
if (!isEnabled)
return;
// Actions size = 2 [vertical speed, horizontal speed] = [-1..1, -1..1] // discrete = [{0, 1, 2}, {0, 1, 2}] = [{-1, 0, 1}...]
float vInput = 0;
float hInput = 0;
@@ -285,6 +293,9 @@ public class AgentControllerV6 : Agent
public override void Heuristic(in ActionBuffers actionsOut)
{
if (!isEnabled)
return;
var discreteActionsOut = actionsOut.DiscreteActions;
discreteActionsOut[0] = 2;