Example #1
0
    private float rateLock = 1;     // starts 1 seconds late, by default.


    void Update()
    {
        if (Time.time > rateLock)
        {
            // player tag.

            if (usePlayerAsFocus)
            {
                GameObject player = GameObject.FindWithTag(Tag.Player);

                if (player)
                {
                    focus = player.transform;
                }
            }

            // bullet factory (?)

            if (bullet && focus)
            {
                // direction / creation

                Vector3 fireDirection = MotionUtility.GetDirection(transform.position, focus.transform.position);

                Transform fireBullet = Instantiate(bullet, transform.position, Quaternion.identity) as Transform;

                // stabilizer

                BulletStabilizer stab = fireBullet.GetComponent <BulletStabilizer>();

                if (!stab)
                {
                    // the bullet stabilizer is required.

                    return;
                }

                // follow?

                if (followFocus)
                {
                    stab.enemy = focus;
                }

                stab.layer = layer;

                stab.direction = fireDirection;

                stab.impulse = impulse;

                // fire rate lock (with variation)

                rateLock = Time.time + Random.Range(rate, rate + rateVariation);
            }
        }
    }
Example #2
0
    void Update()
    {
        // fire rate

        if (Time.time < rateLock || !bullet)
        {
            return;
        }

        // keep the button down, like a machine gun.

        if (Input.GetMouseButton(mouseCode))
        {
            // mouse direction to shoot.

            Vector3 fireDirection = MotionUtility.GetMouseDirectionFrom(transform);

            // bullet creation

            Transform fireBullet = Instantiate(bullet, transform.position, Quaternion.identity) as Transform;

            // connect with bullet stabilizer to apply the impulse.

            BulletStabilizer stab = fireBullet.GetComponent <BulletStabilizer>();

            if (!stab)
            {
                // the bullet stabilizer is required.

                return;
            }

            stab.layer = layer;

            stab.direction = fireDirection;

            stab.impulse = impulse;

            // fire rate lock

            rateLock = Time.time + fireRate;
        }
    }