Пример #1
0
    void FireWeapon()
    {
        bool holdingDownFire = Input.GetKey(KeyCode.Mouse0);

        if (holdingDownFire)
        {
            _shootingTime += Time.deltaTime;

            _fireTimer += Time.deltaTime;
            if (_fireTimer > _fireInterval)
            {
                _fireTimer = 0.0f;

                RaycastHit hitInfo;

                FracturedChunk chunkRaycast = FracturedChunk.ChunkRaycast(_cameraTransform.position, _cameraTransform.forward, out hitInfo);
                if (chunkRaycast)
                {
                    chunkRaycast.FromCenterImpact(hitInfo.point, _fromCenterForce, _fromCenterRadius, true);
                }
                GameAudio.PlayOneShot("GunShot");
            }
        }

        if (!holdingDownFire && _previouslyShooting)
        {
            //if the previous shot was true and current is false
            //the player just stopped shooting
            GameAudio.PlayOneShot("ShellDrop", new Dictionary <string, float>()
            {
                { "Shots", _shootingTime }
            });
            _shootingTime = 0.0f;
        }

        _previouslyShooting = holdingDownFire;
    }
Пример #2
0
    void Update()
    {
        if (ShootMode == Mode.FromCenter)
        {
            // Raycast

            if (Weapon)
            {
                Weapon.GetComponent <Renderer> ().enabled = true;
            }

            bool bShot = ShouldShoot();
            if (bShot)
            {
                _shootingTime += Time.deltaTime;
            }

            bool holdingDownFire = Input.GetKey(KeyCode.Space);
            if (!holdingDownFire && _previousShot)
            {
                //if the previous shot was true and current is false
                //the player just stopped shooting
                GameAudio.PlayOneShot("ShellDrop", new Dictionary <string, float>()
                {
                    { "Shots", _shootingTime }
                });
                _shootingTime = 0.0f;
            }

            if (bShot)
            {
                m_fRecoilTimer = RecoilDuration;
                if (AudioWeaponShot)
                {
                    AudioSource.PlayClipAtPoint(AudioWeaponShot, transform.position, WeaponShotVolume);
                }
            }

            m_bRaycastFound = false;

            RaycastHit hitInfo;

            FracturedChunk chunkRaycast = FracturedChunk.ChunkRaycast(transform.position, transform.forward, out hitInfo);

            if (chunkRaycast)
            {
                m_bRaycastFound = true;

                if (bShot)
                {
                    // Hit it!
                    chunkRaycast.FromCenterImpact(hitInfo.point, FromCenterForce, FromCenterRadius, true);
                }
            }

            _previousShot = holdingDownFire;
        }
        // Mouse-aim

        if (Input.GetMouseButton(0) && Input.GetMouseButtonDown(0) == false)
        {
            this.transform.Rotate(-(Input.mousePosition.y - m_v3MousePosition.y) * MouseSpeed, 0.0f, 0.0f);
            this.transform.RotateAround(this.transform.position, Vector3.up, (Input.mousePosition.x - m_v3MousePosition.x) * MouseSpeed);
        }

        m_v3MousePosition = Input.mousePosition;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            ShootMode = ShootMode == Mode.ExplodeRaycast ? Mode.ShootObjects : Mode.ExplodeRaycast;
        }

        if (ObjectToShoot != null && ShootMode == Mode.ShootObjects)
        {
            // Shoot objects

            if (Weapon)
            {
                Weapon.GetComponent <Renderer>().enabled = false;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                GameObject newObject = GameObject.Instantiate(ObjectToShoot) as GameObject;
                newObject.transform.position                          = this.transform.position;
                newObject.transform.localScale                        = new Vector3(ObjectScale, ObjectScale, ObjectScale);
                newObject.GetComponent <Rigidbody>().mass             = ObjectMass;
                newObject.GetComponent <Rigidbody>().solverIterations = 255;
                newObject.GetComponent <Rigidbody>().AddForce(this.transform.forward * InitialObjectSpeed, ForceMode.VelocityChange);

                //DieTimer dieTimer = newObject.AddComponent<DieTimer>() as DieTimer;
                //dieTimer.SecondsToDie = ObjectLife;
            }
        }

        if (ShootMode == Mode.ExplodeRaycast)
        {
            // Raycast

            if (Weapon)
            {
                Weapon.GetComponent <Renderer>().enabled = true;
            }

            bool bShot = Input.GetKeyDown(KeyCode.Space);

            if (bShot)
            {
                m_fRecoilTimer = RecoilDuration;
                if (AudioWeaponShot)
                {
                    AudioSource.PlayClipAtPoint(AudioWeaponShot, transform.position, WeaponShotVolume);
                }
            }

            m_bRaycastFound = false;

            RaycastHit hitInfo;

            FracturedChunk chunkRaycast = FracturedChunk.ChunkRaycast(transform.position, transform.forward, out hitInfo);

            if (chunkRaycast)
            {
                m_bRaycastFound = true;

                if (bShot)
                {
                    // Hit it!
                    chunkRaycast.Impact(hitInfo.point, ExplosionForce, ExplosionRadius, true);
                }
            }
        }

        // Update recoil

        if (m_fRecoilTimer > 0.0f)
        {
            if (Weapon)
            {
                // Some rudimentary recoil animation here
                Weapon.transform.localPosition = m_v3InitialWeaponPos + new Vector3(0.0f, 0.0f, (-m_fRecoilTimer / RecoilDuration) * RecoilIntensity);
                Weapon.transform.localRotation = m_qInitialWeaponRot * Quaternion.Euler(new Vector3((m_fRecoilTimer / RecoilDuration) * 360.0f * RecoilIntensity * 0.1f, 0.0f, 0.0f));
            }

            m_fRecoilTimer -= Time.deltaTime;
        }
        else
        {
            if (Weapon)
            {
                Weapon.transform.localPosition = m_v3InitialWeaponPos;
                Weapon.transform.localRotation = m_qInitialWeaponRot;
            }
        }

        // Mouse-aim

        if (Input.GetMouseButton(0) && Input.GetMouseButtonDown(0) == false)
        {
            this.transform.Rotate(-(Input.mousePosition.y - m_v3MousePosition.y) * MouseSpeed, 0.0f, 0.0f);
            this.transform.RotateAround(this.transform.position, Vector3.up, (Input.mousePosition.x - m_v3MousePosition.x) * MouseSpeed);
        }

        m_v3MousePosition = Input.mousePosition;
    }