コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        float hAxis = Input.GetAxis("Horizontal");

        rb2d.velocity = new Vector2(acceleration * hAxis + groundVel.x, rb2d.velocity.y);
        if (rb2d.velocity.x < 0.1 && rb2d.velocity.x > -0.1)
        {
            groundVel.x = 0;
        }
        if (grounded && Input.GetKeyDown(KeyCode.W))
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
        }

        cursorPos = new Vector2(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y);
        distanceFromPlayerToCursor = (cursorPos - new Vector2(transform.position.x, transform.position.y)).magnitude;
        if (magicCostIsActive)
        {
            magicCostDistanceMultiplyer = Mathf.Max(minDistanceForMagic, Mathf.Min(distanceFromPlayerToCursor, maxDistanceForMagic));
        }
        else
        {
            magicCostDistanceMultiplyer = 1;
            maxDistanceForMagic         = 20;
        }
        if (distanceFromPlayerToCursor < maxDistanceForMagic)
        {
            canDoMagic = true;
        }
        else
        {
            canDoMagic = false;
        }

        if (canDoMagic)
        {
            if (Input.GetMouseButtonDown(1))
            {
                RaycastHit2D hit = Physics2D.Raycast(new Vector2(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0, 1 << LayerMask.NameToLayer("Links"));
                if (hit.collider != null && hit.collider.gameObject.tag == "Link")
                {
                    LinkController lC = hit.collider.gameObject.GetComponent <LinkController> ();
                    if (lC != null)
                    {
                        GainEnergy(lC.GetPower());
                    }
                    Destroy(hit.collider.gameObject);
                }
            }
            if (Input.GetMouseButton(1))
            {
                RaycastHit2D hit = Physics2D.Raycast(new Vector2(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0, 1 << LayerMask.NameToLayer("Lights"));
                if (hit.collider != null && hit.collider.gameObject.tag == "Light")
                {
                    LightController lightC = hit.collider.gameObject.GetComponent <LightController> ();
                    if (lightC.GetEnergy() >= lightC.eMultiplyer)
                    {
                        lightC.GiveAwayEnergy(lightC.eMultiplyer);
                        GainEnergy(lightC.eMultiplyer);
                    }
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (energy >= 1)
            {
                Instantiate(link);
            }
        }
        Debug.DrawLine(new Vector2(transform.position.x - energy * 0.025f, transform.position.y - 1), new Vector2(transform.position.x + energy * 0.025f, transform.position.y - 1), Color.cyan);
    }