Example #1
0
    public void Shoot(Entity owner, Vector2 startingFacing)
    {
        if (readyToFire)
        {
            audio.Play();
            GameObject bullet = new GameObject("Bullet");
            Destroy(bullet, 10.0f);

            bullet.transform.position = weaponFirePoint.transform.position;
            float angle = Mathf.Atan2(startingFacing.y, startingFacing.x) * Mathf.Rad2Deg;
            currentSpread             = Random.Range(minSpread, maxSpread);
            angle                    += Random.Range(-currentSpread, currentSpread);
            bullet.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

            SpriteRenderer renderer = bullet.AddComponent <SpriteRenderer>();
            renderer.sprite           = Resources.Load <Sprite>("Sprites/Entity/sci-fi-bullet");
            renderer.sortingLayerName = "Bullets";

            BoxCollider2D collider = bullet.AddComponent <BoxCollider2D>();
            collider.isTrigger = true;

            Rigidbody2D rb2d = bullet.AddComponent <Rigidbody2D>();
            rb2d.gravityScale = 0;
            rb2d.velocity     = bullet.transform.right * 20.0f;

            ProjectileData projectileData = bullet.AddComponent <ProjectileData>();
            projectileData.SetProjectileData(owner);
            projectileData.SetDamage(damage);
            projectileData.SetBulletDrop(bulletDrop);

            currentAmmo--;
            readyToFire = false;
        }
    }