Пример #1
0
    private void FixedUpdate()
    {
        InputSensor();

        should_jump = brain.RunNetwork(hsensor, upsensor, downsensor, upBlock, downBlock) > 0.5f;

        //if (should_jump && Time.time > to_allow_juming)
        //{
        //    jump();
        //    to_allow_juming = Time.time + 0.4f;
        //}

        if (should_jump)
        {
            jump();
        }
    }
Пример #2
0
    private void FixedUpdate()
    {
        InputSensors();
        lastPosition = transform.position;


        (a, t) = network.RunNetwork(aSensor, bSensor, cSensor);


        MoveCar(a, t);

        timeSinceStart += Time.deltaTime;

        CalculateFitness();

        //a = 0;
        //t = 0;
    }
Пример #3
0
    void Update()
    {
        Vector2 center = new Vector2(transform.position.x + 5f, 0);
        float   radius = 5f;

        Collider2D[] hitColliders = Physics2D.OverlapCircleAll(center, radius); // Gather obstacles' distances to character as inputs to NNet
        // Debug.Log(hitColliders.Length);
        mace.Clear();
        water.Clear();
        saw.Clear();
        tile.Clear();
        longTile.Clear();
        coin.Clear();
        foreach (Collider2D col in hitColliders)
        {
            if (col.gameObject.name == "Ceiling (1)" || col.gameObject.name == "Character" || col.gameObject.name == "Ceiling (3)" || col.gameObject.name == "Ceiling (2)")
            {
                continue;
            }
            if (col.gameObject.name == "Mace")
            {
                mace.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                mace.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
            else if (col.gameObject.name == "Saw")
            {
                saw.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                saw.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
            else if (col.gameObject.name == "Water")
            {
                water.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                water.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
            else if (col.gameObject.name == "Tile")
            {
                tile.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                tile.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
            else if (col.gameObject.name == "LongTile")
            {
                longTile.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                longTile.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
            else if (col.gameObject.name == "Coin")
            {
                coin.Add((col.gameObject.transform.position.x - transform.position.x) / 10);
                coin.Add((col.gameObject.transform.position.y - transform.position.y) / 10);
            }
        }
        switch (mace.Count)
        {
        case 0:
            mace1X = mace1Y = mace2X = mace2Y = mace3X = mace3Y = 0;
            break;

        case 2:
            mace2X = mace2Y = mace3X = mace3Y = 0;
            mace1X = mace[0];
            mace1Y = mace[1];
            break;

        case 4:
            mace3X = mace3Y = 0;
            mace1X = mace[0];
            mace1Y = mace[1];
            mace2X = mace[2];
            mace2Y = mace[3];
            break;

        default:
            mace1X = mace[0];
            mace1Y = mace[1];
            mace2X = mace[2];
            mace2Y = mace[3];
            mace3X = mace[4];
            mace3Y = mace[5];
            break;
        }
        switch (saw.Count)
        {
        case 0:
            saw1X = saw1Y = saw2X = saw2Y = saw3X = saw3Y = 0;
            break;

        case 2:
            saw2X = saw2Y = saw3X = saw3Y = 0;
            saw1X = saw[0];
            saw1Y = saw[1];
            break;

        case 4:
            saw3X = saw3Y = 0;
            saw1X = saw[0];
            saw1Y = saw[1];
            saw2X = saw[2];
            saw2Y = saw[3];
            break;

        default:
            saw1X = saw[0];
            saw1Y = saw[1];
            saw2X = saw[2];
            saw2Y = saw[3];
            saw3X = saw[4];
            saw3Y = saw[5];
            break;
        }
        switch (water.Count)
        {
        case 0:
            water1X = water1Y = water2X = water2Y = water3X = water3Y = 0;
            break;

        case 2:
            water2X = water2Y = water3X = water3Y = 0;
            water1X = water[0];
            water1Y = water[1];
            break;

        case 4:
            water3X = water3Y = 0;
            water1X = water[0];
            water1Y = water[1];
            water2X = water[2];
            water2Y = water[3];
            break;

        default:
            water1X = water[0];
            water1Y = water[1];
            water2X = water[2];
            water2Y = water[3];
            water3X = water[4];
            water3Y = water[5];
            break;
        }
        switch (tile.Count)
        {
        case 0:
            tile1X = tile1Y = tile2X = tile2Y = 0;
            break;

        case 2:
            tile2X = tile2Y = 0;
            tile1X = tile[0];
            tile1Y = tile[1];
            break;

        default:
            tile1X = tile[0];
            tile1Y = tile[1];
            tile2X = tile[2];
            tile2Y = tile[3];
            break;
        }
        switch (longTile.Count)
        {
        case 0:
            longTile1X = longTile1Y = 0;
            break;

        default:
            longTile1X = longTile[0];
            longTile1Y = longTile[1];
            break;
        }
        switch (coin.Count)
        {
        case 0:
            coin1X = coin1Y = coin2X = coin2Y = 0;
            break;

        case 2:
            coin2X = coin2Y = 0;
            coin1X = coin[0];
            coin1Y = coin[1];
            break;

        default:
            coin1X = coin[0];
            coin1Y = coin[1];
            coin2X = coin[2];
            coin2Y = coin[3];
            break;
        }
        overallFitness = cam.GetComponent <GameManager>().overallFitness;
        if (!ropeAttached)
        {
            isRopeAttached = 0;
        }
        else
        {
            isRopeAttached = 1;
        }
        (shootAngle, resetRope, nothing, cheat) = network.RunNetwork(mace1X, mace1Y, mace2X, mace2Y, mace3X, mace3Y, saw1X, saw1Y, saw2X, saw2Y, saw3X, saw3Y, water1X, water1Y, water2X, water2Y, water3X, water3Y, tile1X, tile1Y, tile2X, tile2Y, longTile1X, longTile1Y, coin1X, coin1Y, coin2X, coin2Y); // neural net
        // Makes 3 possible control options and the rope shoot angle to be outputs of neural network
        outputs[0] = shootAngle;                                                                                                                                                                                                                                                                              // option to shoot the rope with shoot angle
        outputs[1] = resetRope;                                                                                                                                                                                                                                                                               // option to reset the rope
        outputs[2] = nothing;                                                                                                                                                                                                                                                                                 // option to do nothing
        outputs[3] = cheat;                                                                                                                                                                                                                                                                                   // option to render character unkillable
        float temp = 0;                                                                                                                                                                                                                                                                                       // temp variable for comparison

        index = 0;                                                                                                                                                                                                                                                                                            // variable to store the index of highest output
        for (int i = 0; i < 4; i++)                                                                                                                                                                                                                                                                           // compare outputs to select the highest output
        {
            if (outputs[i] > temp)
            {
                temp  = outputs[i];
                index = i;
            }
        }
        // switch (index)
        // {
        //     case 0:
        //         //Debug.Log("setRope");
        //         break;
        //     case 1:
        //         //Debug.Log("resetRope");
        //       //  ResetRope();
        //         break;
        //     case 2:
        //         //Debug.Log("nothing");
        //         break;
        //     case 3:
        //         //Debug.Log("cheat");
        //         //TurnOnShield();
        //         break;
        //     default:
        //         break;
        // }
        if (counter >= ropeJoint.distance && !distanceSet && ropeAttached) //update the rope distance instantly after the rope line is rendered to prevent rope pulling back by distance joint
        {
            ropeJoint.distance = Vector2.Distance(transform.position, ropePositions[0]);
            if (playerMovement.groundPull)
            {
                character.velocity = new Vector2(6f, character.velocity.y);
            }
            else
            {
                character.velocity = new Vector2(character.velocity.x * 1.5f, character.velocity.y);
            }
            distanceSet = true;
        }
        playerPosition = transform.position;
        var worldMousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
        var facingDirection    = worldMousePosition - transform.position;

        aimAngle = Mathf.Atan2(facingDirection.y, facingDirection.x);                        // non neural
        var aimDirection = Quaternion.Euler(0, 0, aimAngle * Mathf.Rad2Deg) * Vector2.right; // normal non neural

        // or 0.88f???
        if (!ropeAttached)
        {
            playerMovement.isSwinging = false;
        }
        else
        {
            playerMovement.isSwinging = true;
        }
        UpdateRopePositions();
        HandleInput(aimDirection);
    }