コード例 #1
0
ファイル: Riffle.cs プロジェクト: Jadw1/LD40
    private void FixedUpdate()
    {
        if (isReloading)
        {
            offset = Mathf.Lerp(offset, -0.1f, 0.1f);
            transform.localPosition = gunPos + transform.up * offset;

            reloadTimeLeft -= Time.deltaTime;

            if (reloadTimeLeft <= 0.0f)
            {
                reloadTimeLeft = 0.0f;
                isReloading    = false;
                PlayerStats.Reload();
            }

            return;
        }

        if (Input.GetButton("Fire1") && Time.time >= timeToFire && PlayerStats.clip > 0 && melee.CanShoot())
        {
            if (fullAmmo)
            {
                fullAmmoCount++;
            }
            fullAmmo = true;

            timeToFire = Time.time + 1 / fireRate;

            // Sound
            audio.PlayOneShot(soundShoot);

            // Hit logic
            Ray        ray = Camera.main.ScreenPointToRay(GetSpread());
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, range, ~ignore))
            {
                if (hit.collider.tag == "Enemy")
                {
                    EnemyStats enemy = hit.collider.GetComponent <EnemyStats>();
                    if (enemy != null)
                    {
                        enemy.dealDamage(PlayerStats.damage);

                        Transform blood = Instantiate(enemyBlood, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)).transform;
                        blood.parent = hit.collider.transform;
                    }
                    else
                    {
                        Debug.LogError("Cant find enemy!!!");
                    }
                }
                else if (hit.collider.tag == "Destroyable")
                {
                    EnemyStats destroyable = hit.collider.GetComponent <EnemyStats>();
                    if (destroyable != null)
                    {
                        destroyable.dealDamage(PlayerStats.damage);
                    }
                    else
                    {
                        Debug.LogError("Cant find destroyable!!!");
                    }
                }


                else
                {
                    Transform hole = Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)).transform;
                    hole.parent = hit.collider.transform;
                }
            }
            PlayerStats.RemoveOneBullet();
            Effect();
        }
        else if (!Input.GetButton("Fire1") && PlayerStats.clip > 0)
        {
            fullAmmo      = false;
            fullAmmoCount = 0;

            DiscardEffect();
        }
        else
        {
            DiscardEffect();
        }
    }