Merge branch 'master' of git@ooftooft.net:Racesm

# Conflicts:
#	Assets/Imported Assets/PolygonCity/Prefabs/Characters/Character_BusinessMan_Shirt_01.prefab
#	Assets/ML-Agents/Timers/Racetrack mini_timers.json
#	Assets/Scenes/Racetrack.unity
#	Assets/Scenes/mini_racetrack3.unity
This commit is contained in:
magn9775
2024-04-24 10:54:42 +02:00
24 changed files with 5772 additions and 1535 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,15 @@ public class AgentControllerV7 : Agent
public int maxStepsPerCheckpoint = 300;
public int distanceBetweenCheckpoints = 5;
public bool ignoreMentalPain = true;
bool isEnabled = true;
public bool isPlaying = false;
[HideInInspector] public bool isFinished = false; // needed for gamemanager
// Start is called before the first frame update
protected override void OnDisable()
{
isEnabled = false;
return;
}
void Start()
{
rb = GetComponent<Rigidbody>();
@@ -47,10 +54,17 @@ public class AgentControllerV7 : Agent
public override void OnEpisodeBegin()
{
if (!isEnabled)
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)
@@ -84,6 +98,13 @@ public class AgentControllerV7 : 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;
// distance to next checkpoint
@@ -124,7 +145,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;
@@ -238,7 +261,16 @@ public class AgentControllerV7 : Agent
if (checkpointsCollected == checkpoints.Count - 1)
{
AddReward(10f);
EndEpisode();
if (isPlaying)
{
isFinished = true;
}
EndEpisode();
}
checkpointsCollected += 1;
@@ -257,7 +289,34 @@ public class AgentControllerV7 : 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());
@@ -265,6 +324,9 @@ public class AgentControllerV7 : Agent
public override void Heuristic(in ActionBuffers actionsOut)
{
if (!isEnabled)
return;
var discreteActionsOut = actionsOut.DiscreteActions;
discreteActionsOut[0] = 2;
@@ -298,6 +360,9 @@ public class AgentControllerV7 : Agent
}
private void OnCollisionEnter(Collision other) {
if (!isEnabled)
return;
// if (other.gameObject.tag == "NPC")
// {
// AddReward(0.1f);

View File

@@ -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)
{
@@ -30,7 +39,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,18 +9,55 @@ 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
void Update()
{
foreach (GameObject player in players)
for (int i = 0; i < players.Count(); i++)
{
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;
}
}
if (playerLaps[i] > lapAmount)
{
player.GetComponent<Rigidbody>().isKinematic = true;
}
}
}
}

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;