Beispiel #1
0
    // Update is called once per frame
    #region Update Function
    public override void Update()
    {
        base.Update();

        if (isPickup || Time.timeScale == 0)
        {
            return;
        }
        if (!weaponEnabled)
        {
            return;
        }

        anim.SetFloat("Charge Speed", 1 / timeToFire);

        #region Setting Properties related To PlayerProperties
        if (playerProperties == null)
        {
            Debug.Log("No PlayerProperties attached");
        }
        else
        {
            reloadSpeed = playerProperties.handSpeed;

            if (currentMagazine > magazineSize)
            {
                currentMagazine = magazineSize;
            }
            if (aiming)
            {
                aimingTimer = .25f / reloadSpeed;
                playerProperties.SetAiming(true);
            }
            else
            {
                playerProperties.SetAiming(false);
            }
        }
        #endregion

        #region Setting canSwitchWeapon

        if (reloadTimer > 0)
        {
            canSwitchWeapon = false;
        }
        else
        {
            if (aimingTimer > 0)
            {
                canSwitchWeapon = false;
            }
            else
            {
                canSwitchWeapon = true;
            }
        }
        #endregion

        #region Setting maxAngleMultiplier and FOV
        if (reloadTimer <= 0)
        {
            if (Input.GetKey(control.aim))
            {
                maxAngleMultiplier = 1 / aimingDivider;
                aiming             = true;
                if (cam.fieldOfView > (30 / zoomAmount))
                {
                    cam.fieldOfView -= 250 * Time.deltaTime;
                }
            }
            else
            {
                maxAngleMultiplier = 1;
                aiming             = false;
                if (cam.fieldOfView < 60)
                {
                    cam.fieldOfView += 250f * Time.deltaTime;
                }
            }
        }
        #endregion

        #region Setting gunAnim bool
        anim.SetBool("Aiming", aiming);
        #endregion

        #region Setting maxPelletSpreadAngle
        maxSpreadAngle  = defaultAngle;
        maxSpreadAngle *= maxAngleMultiplier;
        #endregion

        #region Shooting
        #region Valdating the player meets the shoot requirements
        if (canFireSemiAuto || timeToFireTimer > 0) //Player presses and holds the shoot button, or they have already held the shoot button
        {
            if (timeToFireTimer >= timeToFire)
            {
                if (Input.GetKeyUp(control.fire))//Player releases the shoot button
                {
                    StartRecoil();
                    currentMagazine -= 1f;        //Using 1 ammo
                    lastShot         = Time.time; //Setting lastShot equal to current time
                    timeToFireTimer  = 0;
                    RaycastHit hit;               //Used to return hit data from when one of the rays hits a collider
                    soundCounter = true;
                    anim.SetTrigger("Fire");
                    anim.SetTrigger("Shoot");
                    anim.SetBool("Charging", false);
                    Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone

                    if (!isProjectile)
                    {
                        for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                        {
                            #region Creating a random Ray with an unchanging starting point within a cone
                            Vector3    fireDirection  = cam.transform.forward;
                            Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                            Quaternion randomRotation = Random.rotation;

                            fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                            #endregion
                            pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward);
                        }



                        aud.PlayOneShot(attackSound);
                        float damageCounter = 0;

                        bool isHeadShot = false, isBodyShot = false, hitSomething = false;
                        #region Seeing if rays hit a collider and doing stuff with it
                        for (int i = 0; i < numberOfPellets; i++)
                        {
                            if (Physics.Raycast(pellets[i], out hit))
                            {
                                #region Instantiating Hit Particle facing player
                                var direction    = cam.transform.position - hit.point;
                                var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider

                                Destroy(tempParticle, .5f);
                                #endregion

                                hitSomething = true;
                                if (hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy"))
                                {
                                    #region If ray hits the body
                                    if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                                    {
                                        isBodyShot = true;
                                        health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                        health.TakeDamage(appliedDamage, !isSilenced);
                                        damageCounter += appliedDamage;
                                    }
                                    else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                                    {
                                        isBodyShot = true;
                                        health     = hit.collider.transform.root.GetComponent <HealthSinglePlayer>();
                                        health.TakeDamage(appliedDamage, !isSilenced);
                                        damageCounter += appliedDamage;
                                    }
                                    else
                                    {
                                        Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug
                                    }
                                    #endregion
                                }
                                else if (hit.collider.CompareTag("Enemy Head"))
                                {
                                    #region If ray hits the head
                                    if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())
                                    {
                                        isHeadShot = true;
                                        health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                        health.TakeDamage(appliedDamage * critMultiplier, !isSilenced);
                                        damageCounter += appliedDamage * critMultiplier;
                                    }
                                    #endregion
                                }
                            }
                        }

                        if (isBodyShot && hitSomething)
                        {
                            playerProperties.ShowHitmarker(false, (int)damageCounter);
                        }
                        if (isHeadShot && hitSomething)
                        {
                            playerProperties.ShowHitmarker(true, (int)damageCounter);
                        }
                        #endregion
                    }
                    else
                    {
                        for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                        {
                            #region Creating a random Ray with an unchanging starting point within a cone
                            Vector3    fireDirection  = projectileSpawn.transform.forward;
                            Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                            Quaternion randomRotation = Random.rotation;

                            fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                            #endregion
                            pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward);
                        }

                        aud.PlayOneShot(attackSound);

                        #region Spawning in Projectiles and Shooting
                        for (int i = 0; i < numberOfPellets; i++)
                        {
                            GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction));
                            tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * appliedProjectileSpeed, ForceMode.Impulse);
                            tempProjectile.SendMessage("SetDamage", appliedDamage);
                            tempProjectile.GetComponent <Projectile>().damage           = appliedDamage;
                            tempProjectile.GetComponent <Projectile>().playerProperties = playerProperties;
                            tempProjectile.GetComponent <Projectile>().critHitDamage    = critMultiplier;
                            if (tempProjectile.GetComponent <Projectile>().projectileType == Projectile.Type.Explosive)
                            {
                                ExplosiveProjectile tempExplosive = (ExplosiveProjectile)tempProjectile.GetComponent <Projectile>();
                                tempExplosive.blastRadius = 10;
                            }
                        }
                        #endregion
                    }
                }
            }
            else
            {
                timeToFireTimer        += Time.deltaTime;
                appliedDamage          += rampUpDamageAmount * Time.deltaTime;
                appliedProjectileSpeed += rampUpSpeedAmount * Time.deltaTime;
                if (Input.GetKeyUp(control.fire))
                {
                    aud.Stop();
                    Debug.Log("Released button");
                    if (fireEarly)
                    {
                        StartCoroutine("Recoil");
                        currentMagazine -= 1f;        //Using 1 ammo
                        lastShot         = Time.time; //Setting lastShot equal to current time
                        RaycastHit hit;               //Used to return hit data from when one of the rays hits a collider
                        soundCounter = true;
                        anim.SetTrigger("Fire");
                        anim.SetTrigger("Shoot");
                        anim.SetBool("Charging", false);
                        Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone

                        if (!isProjectile)
                        {
                            for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                            {
                                #region Creating a random Ray with an unchanging starting point within a cone
                                Vector3    fireDirection  = cam.transform.forward;
                                Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                                Quaternion randomRotation = Random.rotation;

                                fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                                #endregion
                                pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward);
                            }



                            aud.PlayOneShot(attackSound);
                            float damageCounter = 0;

                            bool isHeadShot = false, isBodyShot = false, hitSomething = false;
                            #region Seeing if rays hit a collider and doing stuff with it
                            for (int i = 0; i < numberOfPellets; i++)
                            {
                                if (Physics.Raycast(pellets[i], out hit))
                                {
                                    #region Instantiating Hit Particle facing player
                                    var direction    = cam.transform.position - hit.point;
                                    var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider
                                    tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal);
                                    tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit);
                                    Destroy(tempParticle, .5f);
                                    #endregion

                                    hitSomething = true;
                                    if (hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy"))
                                    {
                                        #region If ray hits the body
                                        if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                                        {
                                            isBodyShot = true;
                                            health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                            health.TakeDamage(appliedDamage);
                                            damageCounter += appliedDamage;
                                        }
                                        else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                                        {
                                            isBodyShot = true;
                                            health     = hit.collider.transform.root.GetComponent <HealthSinglePlayer>();
                                            health.TakeDamage(appliedDamage);
                                            damageCounter += appliedDamage;
                                        }
                                        else
                                        {
                                            Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug
                                        }
                                        #endregion
                                    }
                                    else if (hit.collider.CompareTag("Enemy Head"))
                                    {
                                        #region If ray hits the head
                                        if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())
                                        {
                                            isHeadShot = true;
                                            health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                            health.TakeDamage(appliedDamage * critMultiplier);
                                            damageCounter += appliedDamage * critMultiplier;
                                        }
                                        #endregion
                                    }
                                }
                            }

                            if (isBodyShot && hitSomething)
                            {
                                playerProperties.ShowHitmarker(false, (int)damageCounter);
                            }
                            if (isHeadShot && hitSomething)
                            {
                                playerProperties.ShowHitmarker(true, (int)damageCounter);
                            }
                            #endregion
                        }
                        else
                        {
                            for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                            {
                                #region Creating a random Ray with an unchanging starting point within a cone
                                Vector3    fireDirection  = projectileSpawn.transform.forward;
                                Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                                Quaternion randomRotation = Random.rotation;

                                fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                                #endregion
                                pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward);
                            }

                            aud.PlayOneShot(attackSound);

                            #region Spawning in Projectiles and Shooting
                            for (int i = 0; i < numberOfPellets; i++)
                            {
                                GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction));
                                tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * appliedProjectileSpeed, ForceMode.Impulse);
                                tempProjectile.SendMessage("SetDamage", appliedDamage);
                                tempProjectile.SendMessage("SetBlastRadius", 10);
                                tempProjectile.SendMessage("SetPlayerProperties", playerProperties);
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        anim.SetBool("Charging", false);
                    }
                    timeToFireTimer = 0;
                }
                Debug.Log("Holding down while less than charge time");
                if (!aud.isPlaying)
                {
                    if (soundCounter)
                    {
                        soundCounter = false;
                        aud.PlayOneShot(chargeSound);
                    }
                }
                anim.SetBool("Charging", true);
            }
        }
        else
        {
            Debug.Log("Player is not holding shoot button or the timer is 0");
            appliedDamage          = damage;
            appliedProjectileSpeed = projectileSpeed;
            timeToFireTimer        = 0;
            anim.SetBool("Charging", false);
        }
        #endregion

        #region If Player Presses Reload Button
        if (!coroutinePlaying && currentMagazine != magazineSize && playerAmmo.GetAmount(ammoType) > 0)
        {
            if (Input.GetKeyDown(control.reload))
            {
                Reload(false);
            }
        }
        #endregion

        #region Timers
        reloadTimer -= Time.deltaTime;
        aimingTimer -= Time.deltaTime;
        #endregion

        #region Setting Magazine Empty
        magazineEmpty = currentMagazine < 1;
        #endregion

        #region Calling reload If Magazine empty is true and asSoonAsEmpty is true
        if (magazineEmpty && asSoonAsMagEmpty)
        {
            Reload(false);
        }
        #endregion

        #region Updating Update Ammo and Weapon Switch
        updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine);
        weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon);
        #endregion
    }
    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        if (isPickup)
        {
            return;
        }
        reloadSpeed        = playerProperties.handSpeed;
        appliedReloadSpeed = reloadSpeed * reloadSpeedModifier;
        appliedADSSpeed    = reloadSpeed * aimSpeed;

        if (currentMagazine > magazineSize)
        {
            currentMagazine = magazineSize;
        }

        #region Setting Animator Parameters
        anim.SetFloat("Reload Speed", appliedReloadSpeed);
        anim.SetFloat("ADS Speed", appliedADSSpeed);
        #endregion

        #region Setting Aiming
        if (aiming)
        {
            aimingTimer = .25f / reloadSpeed;
            playerProperties.SetAiming(true);
        }
        else
        {
            playerProperties.SetAiming(false);
        }
        #endregion

        #region Setting canSwitchWeapon

        if (reloadTimer > 0)
        {
            canSwitchWeapon = false;
        }
        else
        {
            if (aimingTimer > 0)
            {
                canSwitchWeapon = false;
            }
            else
            {
                canSwitchWeapon = true;
            }
        }
        #endregion

        #region Setting maxAngleMultiplier and FOV
        if (reloadTimer <= 0)
        {
            if (Input.GetKey(control.aim))
            {
                maxAngleMultiplier = 1 / aimingDivider;
                aiming             = true;
                if (cam.fieldOfView > (30 / zoomAmount))
                {
                    cam.fieldOfView -= 250 * Time.deltaTime;
                }
            }
            else
            {
                maxAngleMultiplier = 1;
                aiming             = false;
                if (cam.fieldOfView < 60)
                {
                    cam.fieldOfView += 250f * Time.deltaTime;
                }
            }
        }
        #endregion

        #region Setting gunAnim bool
        anim.SetBool("Aiming", aiming);
        #endregion

        #region Setting maxPelletSpreadAngle
        maxSpreadAngle  = defaultAngle;
        maxSpreadAngle *= maxAngleMultiplier;
        maxSpreadAngle *= accuracyModifier;
        #endregion

        #region Shooting


        if (canFireSemiAuto)
        {
            lastShot = Time.time;
            //The player can shoot
            StartRecoil();
            currentMagazine -= 1f; //Using 1 ammo
            RaycastHit hit;        //Used to return hit data from when one of the rays hits a collider

            anim.SetTrigger("Fire");
            anim.SetTrigger("Shoot");

            Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone

            if (!isProjectile)
            {
                for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                {
                    #region Creating a random Ray with an unchanging starting point within a cone
                    Vector3    fireDirection  = cam.transform.forward;
                    Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                    Quaternion randomRotation = Random.rotation;

                    fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                    #endregion
                    pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward);
                }



                aud.PlayOneShot(attackSound);
                float damageCounter = 0;

                bool isHeadShot = false, isBodyShot = false, hitSomething = false;
                #region Seeing if rays hit a collider and doing stuff with it
                for (int i = 0; i < numberOfPellets; i++)
                {
                    if (Physics.Raycast(pellets[i], out hit))
                    {
                        #region Instantiating Hit Particle facing player
                        var direction    = cam.transform.position - hit.point;
                        var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider
                        tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal);
                        tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit);
                        Destroy(tempParticle, .5f);
                        #endregion

                        hitSomething = true;
                        if (hit.collider.CompareTag("Enemy Body") /*|| hit.collider.CompareTag("Enemy")*/)
                        {
                            #region If ray hits the body
                            if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                            {
                                isBodyShot = true;
                                health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                health.TakeDamage(damage, !isSilenced);
                                damageCounter += damage;
                            }
                            else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                            {
                                isBodyShot = true;
                                health     = hit.collider.transform.root.GetComponent <HealthSinglePlayer>();
                                health.TakeDamage(damage, !isSilenced);
                                damageCounter += damage;
                            }
                            else
                            {
                                Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug
                            }
                            #endregion
                        }
                        else if (hit.collider.CompareTag("Enemy Head"))
                        {
                            #region If ray hits the head
                            if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())
                            {
                                isHeadShot = true;
                                health     = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>();
                                health.TakeDamage(damage * critMultiplier, !isSilenced);
                                damageCounter += damage * critMultiplier;
                            }
                            else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached
                            {
                                isHeadShot = true;
                                health     = hit.collider.transform.root.GetComponent <HealthSinglePlayer>();
                                health.TakeDamage(damage * critMultiplier, !isSilenced);
                                damageCounter += damage * critMultiplier;
                            }
                            else
                            {
                                Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug
                            }
                            #endregion
                        }
                    }
                }

                if (isBodyShot && hitSomething)
                {
                    playerProperties.ShowHitmarker(false, (int)damageCounter);
                }
                if (isHeadShot && hitSomething)
                {
                    playerProperties.ShowHitmarker(true, (int)damageCounter);
                }
                #endregion
            }
            else
            {
                for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays
                {
                    #region Creating a random Ray with an unchanging starting point within a cone
                    Vector3    fireDirection  = projectileSpawn.transform.forward;
                    Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                    Quaternion randomRotation = Random.rotation;

                    fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));
                    #endregion
                    pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward);
                }

                aud.PlayOneShot(attackSound);

                #region Spawning in Projectiles and Shooting
                for (int i = 0; i < numberOfPellets; i++)
                {
                    GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction));
                    tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * 100);
                    tempProjectile.SendMessage("SetDamage", damage);

                    tempProjectile.SendMessage("SetPlayerProperties", playerProperties);
                }
                #endregion
            }
        }


        #endregion

        #region If Player Presses Reload Button
        if (reloadTimer <= 0)
        {
            if (Input.GetKeyDown(control.reload))
            {
                Reload();
            }
        }
        #endregion

        #region Timers
        reloadTimer -= Time.deltaTime;
        aimingTimer -= Time.deltaTime;
        #endregion

        #region Setting Magazine Empty
        magazineEmpty = currentMagazine < 1;
        #endregion

        #region Updating Update Ammo and Weapon Switch
        updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine);
        weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon);
        #endregion
    }
Beispiel #3
0
    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        if (isPickup || !weaponEnabled)
        {
            return;
        }
        if (Time.timeScale == 0)
        {
            return;
        }

        reloadSpeed = playerProperties.handSpeed;

        appliedReloadSpeed = reloadSpeed * reloadSpeedModifier;
        appliedADSSpeed    = reloadSpeed * aimSpeed;
        //If player is aiming, set the aiming timer relative to the reload speed
        if (aiming)
        {
            aimingTimer = .25f / appliedADSSpeed; //As reload speed increases, the aiming timer decreases, so it is equal to the animation speed
            playerProperties.SetAiming(true);
            anim.SetFloat("ADS Speed", appliedADSSpeed);
            anim.SetLayerWeight(2, .05f);
        }
        else
        {
            anim.SetLayerWeight(2, .2f);
        }
        //Sets canSwitchWeapon
        if (reloadTimer > 0)         //If the player is still reloading
        {
            canSwitchWeapon = false; //They can't switch weapons
        }
        else
        {                                //The player is not reloading
            if (aimingTimer > 0)         //But the player is either aiming, or the aiming animation is still playing
            {
                canSwitchWeapon = false; //They can't switch weapons
            }
            else//Not aiming or reloading
            {
                canSwitchWeapon = true;//They can switch weapons
            }
        }


        if (currentMagazine > magazineSize)
        {
            currentMagazine = magazineSize;
        }

        //Sets the angleMultiplier depending on whether or not the player is aiming
        if (reloadTimer <= 0)              //If not reloading
        {
            if (Input.GetKey(control.aim)) //If user presses the aim button
            {
                maxAngleMultiplier = 1 / aimingDivider;
                aiming             = true; //If the user is pressing the aim button, then aiming is true
                if (cam.fieldOfView > (30 / zoomAmount))
                {
                    cam.fieldOfView -= 250 * Time.deltaTime;
                }
            }
            else   //The user isn't pressing the aim button
            {
                aiming = false;
                if (cam.fieldOfView < 60)
                {
                    cam.fieldOfView += 250 * Time.deltaTime;
                }
            }
        }

        if (coroutinePlaying)
        {
            aiming = false;
        }

        //Plays the aiming animation depending on whether or not the player is aiming
        anim.SetBool("Aiming", aiming);
        maxSpreadAngle  = defaultAngle;
        maxSpreadAngle *= maxAngleMultiplier; //Multiplies the maxBulletSpreadAngle by the multiplier, which is changed throughout the script depending on different things
        maxSpreadAngle *= accuracyModifier;

        //Actually Shooting
        #region Shooting
        if (canFireAuto)
        {
            currentMagazine -= 1f;        //Subract one from the current gun magazine
            lastShot         = Time.time; //lastShot is set equal to the current time, to compare in for the next shot
            RaycastHit hit;               //Creates a raycasthit called hit


            anim.SetTrigger("Shoot");//Triggers the firing animation

            //vvvvvvvv Complicated math to create a random ray inside a cone pointing forward vvvvv
            Vector3    fireDirection  = cam.transform.forward;
            Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
            Quaternion randomRotation = Random.rotation;

            fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle));

            //^^^^^^^ Complicated math to create a random ray inside a cone pointing forward ^^^^^^

            //Creates a new ray originating at the cameras position, and pointing in the random direction that was created above
            Ray ray = new Ray(cam.transform.position, fireRotation * Vector3.forward);

            //Plays the gunShot sound once
            aud.PlayOneShot(attackSound);

            if (Physics.Raycast(ray, out hit))                                                              //If the raycast hits a collider, returns information about the collider in a variable called hit
            {
                var direction    = cam.transform.position - hit.point;                                      //Sets a direction that faces the player
                var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Sets tempParticle equal to an instantiated hit particle facing the player
                tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal);
                tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit);
                Destroy(tempParticle, 1);                                                //Destroys the particle object after a second, to get rid of unnecessary objects

                if (hit.collider.CompareTag("Enemy Body"))                               //if the object hit is tagged with "Enemy Body"
                {
                    if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>()) //If the collider has a HealthSinglePlayer component attached
                    {
                        playerProperties.ShowHitmarker(false, (int)damage);
                        health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); //Sets the health to the health script attached to the gameObject hit
                        health.TakeDamage(damage, !isSilenced);                                   //Calls the takeDamage method using damage as the argument
                    }
                }
                else if (hit.collider.CompareTag("Enemy Head")) //If the object hit is tagged with "Enemy head"
                {
                    if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())
                    {
                        playerProperties.ShowHitmarker(true, (int)(damage * critMultiplier));
                        health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); //Same as before
                        health.TakeDamage(damage * critMultiplier, !isSilenced);                  //Deals double damage
                    }
                }
            }
            StartRecoil();
        }
        #endregion

        UpdateReticle();

        //Reload call when "R" is pressed
        if (!coroutinePlaying)                                                                                                //Not reloading a
        {
            if (Input.GetKeyDown(control.reload) && (currentMagazine != magazineSize && playerAmmo.GetAmount(ammoType) != 0)) //Player presses reload button
            {
                Reload(true);                                                                                                 //Call the reload method
            }
        }

        //Sets the angleMultiplier
        if (!aiming)
        { //If the player isn't aiming. Angle multiplier is changed higher up if the player is aiming
            playerProperties.SetAiming(false);
            if (Input.GetKey(control.fire))
            {                                            //The fire button is being held down
                if (bulletSpreadTimer < 5)
                {                                        //The bulletSpreadTimer is less than 5 (seconds)
                    bulletSpreadTimer += Time.deltaTime; //Continue to increase the timer
                }
                if (bulletSpreadTimer < 6)
                {
                    maxAngleMultiplier = angleSpreadAdder + bulletSpreadTimer; //Increase multiplier
                }
            }

            else
            {                          //The fire button isn't held down
                bulletSpreadTimer = 0; //Set the timer to 0
                if (maxAngleMultiplier > 1)
                {
                    maxAngleMultiplier -= Time.deltaTime; //Decrease the angleMultiplier over time
                }
            }
        }

        //Sets magazine empty to whether or not currentMagaine is less than 1
        magazineEmpty = (currentMagazine < 1);

        if (magazineEmpty && asSoonAsMagEmpty && !coroutinePlaying)
        {
            Reload(true);
        }

        if (magazineEmpty)
        {
            anim.SetBool("Gun Empty", true);
        }
        else
        {
            anim.SetBool("Gun Empty", false);
        }


        //Decreasing general timers
        reloadTimer -= Time.deltaTime;
        aimingTimer -= Time.deltaTime;


        //Sending reloadSpeed float to the animator
        // armAnim.SetFloat("Reload Speed", reloadSpeed);
        anim.SetFloat("Reload Speed", appliedReloadSpeed);

        updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine);
        weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon);
    }
Beispiel #4
0
    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        if (Time.timeScale == 0 || isPickup || !weaponEnabled)
        {
            return;
        }

        if (currentMagazine > magazineSize)
        {
            currentMagazine = magazineSize;
        }


        #region Setting Aiming
        if (aiming)
        {
            aimingTimer = .25f / reloadSpeed;
            playerProperties.SetAiming(true);
        }
        else
        {
            playerProperties.SetAiming(false);
        }
        #endregion

        #region Setting canSwitchWeapon

        if (reloadTimer > 0)
        {
            canSwitchWeapon = false;
        }
        else
        {
            if (aimingTimer > 0)
            {
                canSwitchWeapon = false;
            }
            else
            {
                canSwitchWeapon = true;
            }
        }
        #endregion

        #region Setting maxAngleMultiplier and FOV
        if (reloadTimer <= 0)
        {
            if (Input.GetKey(control.aim))
            {
                maxAngleMultiplier = 1 / aimingDivider;
                aiming             = true;
                if (cam.fieldOfView > (30 / zoomAmount))
                {
                    cam.fieldOfView -= 250 * Time.deltaTime;
                }
            }
            else
            {
                maxAngleMultiplier = 1;
                aiming             = false;
                if (cam.fieldOfView < 60)
                {
                    cam.fieldOfView += 250f * Time.deltaTime;
                }
            }
        }
        #endregion

        #region Setting gunAnim bool
        anim.SetBool("Aiming", aiming);
        #endregion

        #region Setting maxSpreadAngle
        maxSpreadAngle  = defaultAngle;
        maxSpreadAngle *= maxAngleMultiplier;
        #endregion

        #region Shooting
        if (canFireSemiAuto && !burstRoutinePlaying)
        {
            StartCoroutine("Burst");
        }
        #endregion

        if (Input.GetKeyDown(control.fire) && magazineEmpty)
        {
            if (whenFiringEmptyMag)
            {
                Reload();
            }
        }



        #region If Player Presses Reload Button
        if (reloadTimer <= 0)
        {
            if (Input.GetKeyDown(control.reload) && currentMagazine != magazineSize)
            {
                Reload();
            }
        }
        #endregion

        #region Timers
        reloadTimer -= Time.deltaTime;
        aimingTimer -= Time.deltaTime;
        #endregion

        #region Setting Magazine Empty
        magazineEmpty = (currentMagazine < 1);
        #endregion

        if (magazineEmpty && asSoonAsMagEmpty)
        {
            Reload();
        }

        #region Updating Update Ammo and Weapon Switch
        updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine);
        weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon);
        #endregion
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        reloadSpeed = playerPropterties.handSpeed;

        #region Setting Aiming
        if (aiming)
        {
            aimingTimer = .25f / reloadSpeed;
            playerPropterties.SetAiming(true);
        }
        else
        {
            playerPropterties.SetAiming(false);
        }
        #endregion

        #region Setting canSwitchWeapon

        if (reloadTimer > 0)
        {
            canSwitchWeapon = false;
        }
        else
        {
            if (aimingTimer > 0)
            {
                canSwitchWeapon = false;
            }
            else
            {
                canSwitchWeapon = true;
            }
        }
        #endregion

        #region Setting maxAngleMultiplier and FOV
        if (reloadTimer <= 0)
        {
            if (Input.GetAxis("Aim") != 0)
            {
                maxAngleMutliplier = 1 / aimingAngleDivider;
                aiming             = true;
                if (cam.fieldOfView > (30 / zoomAmount))
                {
                    cam.fieldOfView -= 250 * Time.deltaTime;
                }
            }
            else
            {
                maxAngleMutliplier = 1;
                aiming             = false;
                if (cam.fieldOfView < 60)
                {
                    cam.fieldOfView += 250f * Time.deltaTime;
                }
            }
        }
        #endregion

        #region Setting gunAnim bool
        gunAnim.SetBool("Aiming", aiming);
        #endregion

        #region Setting maxPelletSpreadAngle
        maxProjectileSpreadAngle  = defaultAngle;
        maxProjectileSpreadAngle *= maxAngleMutliplier;
        #endregion

        #region Shooting
        #region Valdating the player meets the shoot requirements
        if (Input.GetButton("Fire"))                     //Player presses the shoot button
        {
            if (currentGunMagazine > 0)                  //Player has enough ammo
            {
                if (reloadTimer <= 0)                    //Player is not reloading
                {
                    if (Time.time > fireRate + lastShot) //They are within the fireRate
                    #endregion
                    {
                        StartCoroutine("Recoil");
                        //The player can shoot
                        currentGunMagazine -= 1f;        //Using 1 ammo
                        lastShot            = Time.time; //Setting lastShot equal to current time

                        gunAnim.SetTrigger("Fire");
                        gunAnim.SetTrigger("Shoot");



                        #region Creating a random Ray with an unchanging starting point within a cone
                        Ray        cameraRay = new Ray(transform.position, transform.forward);
                        Vector3    fireDirection;
                        RaycastHit hit;
                        if (Physics.Raycast(cameraRay, out hit))
                        {
                            if (!aiming)
                            {
                                fireDirection = hit.point - projectileSpawnTransform.transform.position;
                            }
                            else
                            {
                                fireDirection = hit.point - transform.root.gameObject.transform.position;
                            }
                        }
                        else
                        {
                            fireDirection = projectileSpawnTransform.transform.forward;
                        }


                        Quaternion fireRotation   = Quaternion.LookRotation(fireDirection);
                        Quaternion randomRotation = Random.rotation;

                        fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxProjectileSpreadAngle));
                        Ray fireRay = new Ray(projectileSpawnTransform.transform.position, fireRotation * Vector3.forward);
                        #endregion

                        projectileRigidBody = projectilePrefab.GetComponent <Rigidbody>();

                        aud.PlayOneShot(gunShot);

                        #region Shooting Projectile in Direction of Ray

                        GameObject tempProjectile = Instantiate(projectilePrefab, projectileSpawnTransform.transform.position, (Quaternion)projectileSpawnTransform.rotation);
                        tempProjectile.SendMessage("SetDamage", projectileDamage);
                        tempProjectile.SendMessage("SetBlastRadius", blastRadius);
                        tempProjectile.SendMessage("SetPlayerProperties", playerPropterties);

                        projectileRigidBody      = tempProjectile.GetComponent <Rigidbody>();
                        projectileRigidBody.mass = projectileMass;
                        projectileRigidBody.AddForce(fireRay.direction * shootSpeed);

                        Destroy(tempProjectile, 20f);
                        #endregion
                    }
                }
            }
        }
        #endregion

        #region If Player Presses Reload Button
        if (reloadTimer <= 0)
        {
            if (Input.GetButtonDown("Reload"))
            {
                Reload();
            }
        }
        #endregion

        #region Timers
        reloadTimer -= Time.deltaTime;
        aimingTimer -= Time.deltaTime;
        #endregion

        #region Setting Magazine Empty
        magazineEmpty = currentGunMagazine < 1;
        #endregion

        #region Updating Update Ammo and Weapon Switch
        updateAmmo.ChangeAmountObject(ammoAmount, currentGunMagazine);
        weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon);
        #endregion

        if (magazineEmpty)
        {
            gunAnim.SetBool("Empty", true);
        }
        else
        {
            gunAnim.SetBool("Empty", false);
        }
    }