Example #1
0
    // FixedUpdate is called every physics tick
    void FixedUpdate()
    {
        float percent = playerInput.CurrentInput.MoveDir.magnitude;

        if (playerInput.CurrentInput.MoveDir.magnitude > 0)
        {
            percent = energyManager.SpendThrustEnergy(percent);
        }

        /* movement handling */
        if (playerInput.CurrentInput.MoveDir == Vector2.zero)
        {
            // decelerate if no input
            rb.velocity *= 1 - decelerationPerTick;
        }
        else
        {
            rb.AddForce(playerInput.CurrentInput.MoveDir * thrust * percent);

            // clamp velocity to max speed
            rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxControlledVelocity);
        }

        /* weapon handling */
        if (fire1 && Time.time >= (time + fireWait))
        {
            if (energyManager.SpendBulletEnergy())
            {
                gunSoundSource.Play();
                Poolable        obj    = bulletPool.Pop();
                BulletBehaviour bullet = obj.GetComponent <BulletBehaviour>();

                // sin and cosine for current aim direction
                float sin = Mathf.Sin(rb.rotation * Mathf.Deg2Rad);
                float cos = Mathf.Cos(rb.rotation * Mathf.Deg2Rad);

                // find offset for gun barrel
                Vector2 b      = barrels[barrelNum];
                Vector2 offset = new Vector2(b.x * cos - b.y * sin,
                                             b.x * sin + b.y * cos);
                if (++barrelNum >= barrels.Length)
                {
                    barrelNum = 0;
                }

                // fire bullet from offset position
                bullet.transform.position = rb.position + offset;
                bullet.Fire(aimDir);
                time = Time.time;
            }

            // reset input bool
            fire1 = false;
        }
    }