Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (!weightsSet)
        {
            myNeuralNetwork.SetWeights(myAgent.myC.genes);
            weightsSet = true;
        }

        if (transform.position.x <= -15)
        {
            transform.position = new Vector3(-15, transform.position.y, 0);
        }
        if (transform.position.x >= 15)
        {
            transform.position = new Vector3(15, transform.position.y, 0);
        }
        if (transform.position.y >= 10)
        {
            transform.position = new Vector3(transform.position.x, 10, 0);
        }
        if (transform.position.y <= -2.5f)
        {
            transform.position = new Vector3(transform.position.x, -2.5f, 0);
        }

        distance = Vector3.Distance(transform.position, target.transform.position);


        if (reached)
        {
            myRB.Sleep();
            if (transform.position.y < target.transform.position.y)
            {
                myAgent.SetFitness(20 - distance);
            }
            else
            {
                myAgent.SetFitness(20 - distance);
            }
        }
        else
        {
            if (myAgent != null)
            {
                //if(actionCounter < actionTime) {
                //    actionCounter += Time.deltaTime;
                //    PerformAction();
                //}else if(index >= 0) {
                //    index++;
                //    if (index < geneCount) {
                //        actionTime = myAgent.myC.genes[index].time;
                //        myAction = myAgent.myC.genes[index].action;
                //        currentThrust = myAgent.myC.genes[index].thrust;
                //        currentRot = myAgent.myC.genes[index].rotAmount;
                //        actionCounter = 0;
                //    }
                //}else
                //    index++;


                float angle = transform.eulerAngles.z % 360f;

                if (angle < 0f)
                {
                    angle += 360f;
                }

                Vector2 deltaVector = (target.transform.position - transform.position).normalized;

                float rad = Mathf.Atan2(deltaVector.y, deltaVector.x);
                rad *= Mathf.Rad2Deg;

                rad = rad % 360;
                if (rad < 0)
                {
                    rad = 360 + rad;
                }

                rad = 90f - rad;
                if (rad < 0f)
                {
                    rad += 360f;
                }

                rad  = 360 - rad;
                rad -= angle;

                if (rad < 0)
                {
                    rad = 360 + rad;
                }

                if (rad >= 180f)
                {
                    rad  = 360 - rad;
                    rad *= -1f;
                }

                rad *= Mathf.Deg2Rad;


                //if (actionCounter > actionTime) {

                actionCounter = 0;
                inputs[0]     = rad / (Mathf.PI);

                float[] output = myNeuralNetwork.ProcessInputs(inputs);

                myRB.velocity        = 2.5f * transform.up;
                myRB.angularVelocity = 250 * output[0];
                if (myRB.angularVelocity > 500)
                {
                    myRB.angularVelocity = 500;
                }
                else if (myRB.angularVelocity < -500)
                {
                    myRB.angularVelocity = -500;
                }

                if (output[0] == lastOutput[0])
                {
                    constant = true;
                }
                else
                {
                    constant = false;
                }

                lastOutput = output;

                if (!constant)
                {
                    myAgent.SetFitness(20 - distance);
                }
                else
                {
                    myAgent.SetFitness(20 - distance);
                }
                //}else
                //    actionCounter += Time.deltaTime;
            }
        }

        myAgent.myC.SetWeights(myNeuralNetwork.GetWeights());
    }