// Update is called once per frame void FixedUpdate() { if (!crashed) { if (randomStart) { rend.material.color = new Color(1, 0, 0); randomStart = false; transform.Translate(new Vector3(0, 0.01f, 0)); } if (champion) { rend.material.color = new Color(0, 1, 0); champion = false; transform.Translate(new Vector3(0, 0.015f, 0)); } if (wasLoaded) { rend.material.color = new Color(0, 0, 1); wasLoaded = false; transform.Translate(new Vector3(0, 0.02f, 0)); } // must hit a lap gate at least every 10 seconds or die lapGateTime -= Time.deltaTime; if (lapGateTime < 0) { wallHit(); } //gets the left and right of the car var right45 = Quaternion.Euler(0, 45, 0) * transform.forward; var left45 = Quaternion.Euler(0, -45, 0) * transform.forward; ///raycasts to check how far the nearest wall is to the car in the direction RaycastHit forwardRay; //forward raycast if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out forwardRay, 10, layerMask)) { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * forwardRay.distance, Color.red); forwardDistance = forwardRay.distance; } else { forwardDistance = 10.0f; } //left raycast RaycastHit leftRay; if (Physics.Raycast(transform.position, left45, out leftRay, 10, layerMask)) { Debug.DrawRay(transform.position, left45 * leftRay.distance, Color.blue); leftDistance = leftRay.distance; } else { leftDistance = 10.0f; } //right raycast RaycastHit rightRay; if (Physics.Raycast(transform.position, right45, out rightRay, 10, layerMask)) { Debug.DrawRay(transform.position, right45 * rightRay.distance, Color.blue); rightDistance = rightRay.distance; } else { rightDistance = 10.0f; } //---puts the raycast lengths between 0 and 1 forwardDistance /= 10; leftDistance /= 10; rightDistance /= 10; // ------------------------ Put Neural network below this ------------------------ float[] inputs = new float[3]; //sets the inputs inputs[0] = forwardDistance; inputs[1] = leftDistance; inputs[2] = rightDistance; //runs the inputs through the network float[] outputs = neuralNetwork.FeedForward(inputs); // ------------------------ Put Neural network above this ------------------------ //gets the outputs from the neural network outputs[0] *= turnSpeed; outputs[1] *= acceleration; //add the outputs to the related variable rotation += outputs[0]; velocity = outputs[1]; //movement speed clamps if (velocity > maxSpeed) { velocity = maxSpeed; } if (velocity < -maxSpeed) { velocity = -maxSpeed; } // rotation speed clamps if (rotation > maxTurnSpeed) { rotation = maxTurnSpeed; } if (rotation < -maxTurnSpeed) { rotation = -maxTurnSpeed; } //moves the car according to the outputs received transform.Rotate(new Vector3(0, outputs[0], 0) * Time.deltaTime); transform.Translate(new Vector3(0, 0, outputs[1]) * Time.deltaTime); //adds the distance traveled by the car to the fitness score distance = transform.position - lastPos; distanceTravelled = Vector3.Magnitude(distance); lastPos = transform.position; // add the fitness score to the neural network -> the further it moves the higher the fitness score becomes increaseFitness(); } }