コード例 #1
0
    // Use this for initialization
    void Start()
    {
        lane      = LANE.TWO;
        prev_lane = LANE.TWO;

        cocoState = STATE.IDLE;

        rigidBody2D = GetComponent <Rigidbody2D>();

        targetPositionX = 0;
        prevPositionX   = 0;

        interpolateTimer = 0;
    }
コード例 #2
0
    /*
     * Fungsi untuk memasukkan platform
     * pada lane tertentu ke dalam level
     */
    public void AddPlatform(LANE lane)
    {
        GameObject platform = new GameObject();

        // atur posisi sesuai lane
        float x_pos = -1.0f;

        if (lane == LANE.ONE)
        {
            x_pos = -3.0f;
        }
        else if (lane == LANE.TWO)
        {
            x_pos = -1.0f;
        }
        else if (lane == LANE.THREE)
        {
            x_pos = 1.0f;
        }
        else if (lane == LANE.FOUR)
        {
            x_pos = 3.0f;
        }

        platform.transform.position = new Vector3(x_pos, 4.5f, 0);

        // buat parent platform menjadi GameObject level
        platform.transform.parent = transform.root;

        SpriteRenderer sr = platform.AddComponent <SpriteRenderer>();

        sr.sprite = Resources.Load <Sprite>("level3_cloud");

        // ttambahkan komponen BoxCollider2D agar coco bisa berpijak
        BoxCollider2D collider = platform.AddComponent <BoxCollider2D>();

        collider.size    = new Vector2(2.0f, 0.2f);
        collider.center  = new Vector2(0, -0.2f);
        collider.enabled = false;// disable, agar coco dapat melompat ke atasnya

        // masukkan dalam list
        platformPool.Add(platform);
    }
コード例 #3
0
    // Update is called once per frame
    void Update()
    {
        switch (cocoState)
        {
        // pada saat idle cek tombol yang ditekan
        case STATE.IDLE:

            if (Input.GetKeyUp(KeyCode.A))
            {
                lane = LANE.ONE;
                rigidBody2D.AddForce(new Vector2(0, 350));
            }

            if (Input.GetKeyUp(KeyCode.S))
            {
                lane = LANE.TWO;
                rigidBody2D.AddForce(new Vector2(0, 350));
            }

            if (Input.GetKeyUp(KeyCode.D))
            {
                lane = LANE.THREE;
                rigidBody2D.AddForce(new Vector2(0, 350));
            }

            if (Input.GetKeyUp(KeyCode.F))
            {
                lane = LANE.FOUR;
                rigidBody2D.AddForce(new Vector2(0, 350));
            }

            if (prev_lane != lane)
            {
                prevPositionX = transform.position.x;

                if (lane == LANE.ONE)
                {
                    targetPositionX = -3.0f;
                }

                if (lane == LANE.TWO)
                {
                    targetPositionX = -1.0f;
                }

                if (lane == LANE.THREE)
                {
                    targetPositionX = 1.0f;
                }

                if (lane == LANE.FOUR)
                {
                    targetPositionX = 3.0f;
                }

                interpolateTimer = 0;
                prev_lane        = lane;

                cocoState = STATE.JUMPING;
            }

            break;

        case STATE.JUMPING:

            // pada saat melompat, interpolasi posisi
            if (interpolateTimer < 1.0f)
            {
                interpolateTimer += Time.deltaTime;
                float positionX = Mathf.Lerp(prevPositionX, targetPositionX, interpolateTimer);
                transform.position = new Vector2(positionX, transform.position.y);
            }
            else
            {
                cocoState        = STATE.IDLE;
                interpolateTimer = 0;
            }

            break;
        }
    }