コード例 #1
0
    void CastSkill()
    {
        skillSprite = Resources.Load <Sprite>("skillSprite");
        GameObject gameObject     = new GameObject();
        var        spriteRenderer = gameObject.AddComponent <SpriteRenderer>();
        var        r1             = gameObject.AddComponent <CircleCollider2D>().radius;
        var        r2             = GetComponent <CircleCollider2D>().radius;

        spriteRenderer.sprite           = skillSprite;
        skillEffect                     = Sprite.Instantiate(skillSprite, rb.position, Quaternion.identity);//在这里创建Sprite
        gameObject.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);

        Vector3 mousePosInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        var     rbpos3d         = new Vector3(rb.position.x, rb.position.y, 0);
        var     pos             = mousePosInWorld - rbpos3d;

        pos.z = 0;
        gameObject.transform.position = rbpos3d + pos.normalized * (r1 + r2);

        var skillRigid = gameObject.AddComponent <Rigidbody2D>();

        skillRigid.mass         = 0;
        skillRigid.velocity     = pos.normalized * skillSpeed;
        skillRigid.gravityScale = 0;
        var skillController = gameObject.AddComponent <SkillController>();

        skillController.duration = 0.3f;

        //audio
        musicController.useskill();
    }