This commit is contained in:
2024-05-06 14:12:52 +02:00
parent a756f5e9aa
commit fc9d571b0b
2 changed files with 3 additions and 22 deletions

View File

@@ -91,8 +91,6 @@ public class AgentController: Agent
// transform.position.z + rng // transform.position.z + rng
// ); // );
} }
public override void CollectObservations(VectorSensor sensor) public override void CollectObservations(VectorSensor sensor)
@@ -170,17 +168,11 @@ public class AgentController: Agent
totalReward -= 0.0018f; totalReward -= 0.0018f;
float forwardSpeed = Vector3.Dot(transform.forward, rb.velocity); float forwardSpeed = Vector3.Dot(transform.forward, rb.velocity);
// Calculate how close the car is to top speed
// as a number from zero to one
float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed); float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed);
// Use that to calculate how much torque is available
// (zero torque at top speed)
float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor); float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor);
// …and to calculate how much to steer
// (the car steers more gently at top speed)
float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor); float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor);
// Check whether the user input is in the same direction // Check whether the user input is in the same direction

View File

@@ -43,26 +43,15 @@ public class PlayerController : MonoBehaviour
float vInput = Input.GetAxis("Vertical"); float vInput = Input.GetAxis("Vertical");
float hInput = Input.GetAxis("Horizontal"); float hInput = Input.GetAxis("Horizontal");
// Calculate current speed in relation to the forward direction of the car
// (this returns a negative number when traveling backwards)
float forwardSpeed = Vector3.Dot(transform.forward, rigidBody.velocity); float forwardSpeed = Vector3.Dot(transform.forward, rigidBody.velocity);
// Calculate how close the car is to top speed
// as a number from zero to one
float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed); float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed);
// Use that to calculate how much torque is available
// (zero torque at top speed)
float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor); float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor);
// …and to calculate how much to steer
// (the car steers more gently at top speed)
float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor); float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor);
// Check whether the user input is in the same direction
// as the car's velocity
bool isAccelerating = Mathf.Sign(vInput) == Mathf.Sign(forwardSpeed); bool isAccelerating = Mathf.Sign(vInput) == Mathf.Sign(forwardSpeed);
bool isStopping = vInput == 0; // range bool isStopping = vInput == 0; // range