コード例 #1
0
    public void DoDamage(int hitDirection = 0)
    {
        if (_camera == null)
        {
            return;
        }
        if (_gameSceneManager == null)
        {
            return;
        }

        // Local Variables
        Ray        ray;
        RaycastHit hit;
        bool       isSomethingHit = false;

        ray = _camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

        isSomethingHit = Physics.Raycast(ray, out hit, 1000.0f, 1 << _aiBodyPartLayer);

        if (isSomethingHit)
        {
            AIStateMachine stateMachine = _gameSceneManager.GetAIStateMachine(hit.rigidbody.GetInstanceID());
            Debug.Log("Body part is: " + hit.rigidbody.GetInstanceID());
            if (stateMachine)
            {
                Debug.Log("This has worked");
                stateMachine.TakeDamage(hit.point, ray.direction * 1.0f, 50, hit.rigidbody, this, 0);
            }
        }
    }
コード例 #2
0
    public void DoDamage(int hitDirection = 0)
    {
        if (_camera == null)
        {
            return;
        }
        if (_gameSceneManager == null)
        {
            return;
        }

        Ray        ray;
        RaycastHit hit;
        bool       isSomethingHit = false;

        ray = _camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

        isSomethingHit = Physics.Raycast(ray, out hit, 1000, 1 << _aiBodyPartLayer);

        if (isSomethingHit)
        {
            //used to search dictionary to find which zombie has been hit with ray
            AIStateMachine stateMachine = _gameSceneManager.GetAIStateMachine(hit.rigidbody.GetInstanceID());
            if (stateMachine)
            {
                //parameters taken from current weapon
                stateMachine.TakeDamage(hit.point, ray.direction * 1.0f, 100, hit.rigidbody, this, 0);
            }
        }
    }
コード例 #3
0
ファイル: CharacterManager.cs プロジェクト: Auggst/FPS-Demo
    // -------------------------------------------------------------------------
    // 类	    :	DoDamage
    // 介绍		:	攻击
    // -------------------------------------------------------------------------
    public void DoDamage(int hitDirection = 0)
    {
        if (_camera == null)
        {
            return;
        }
        if (_gameSceneManager == null)
        {
            return;
        }

        Ray        ray;
        RaycastHit hit;
        bool       isSomethingHit = false;

        ray = _camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0.0f));

        isSomethingHit = Physics.Raycast(ray, out hit, 1000.0f, 1 << _aiBodyPartLayer);

        if (isSomethingHit)
        {
            AIStateMachine stateMachine = _gameSceneManager.GetAIStateMachine(hit.rigidbody.GetInstanceID());
            if (stateMachine)
            {
                stateMachine.TakeDamage(hit.point, ray.direction * 5.0f, 25, hit.rigidbody, this, 0);
            }
        }
    }
コード例 #4
0
 private void OnTriggerStay(Collider other)
 {
     if (AIManager.Instance)
     {
         if (currentIntersectingEnemies.Count > 0)
         {
             // Try to get an AI State Machine from the AI Manager
             AIStateMachine _stateMachine = AIManager.Instance.GetAIStateMachine(other.GetInstanceID());
             if (currentIntersectingEnemies.Contains(_stateMachine))
             {
                 FMODUnity.RuntimeManager.PlayOneShot(playerAttack);
                 // Do damage to the enemy
                 _stateMachine.TakeDamage(_playerControl.Damage);
                 // Remove it from the list
                 currentIntersectingEnemies.Remove(_stateMachine);
                 // Check if we're back at 0
                 if (currentIntersectingEnemies.Count == 0)
                 {
                     // Disable this game object
                     gameObject.SetActive(false);
                 }
             }
         }
     }
 }
コード例 #5
0
    public void DecapitatePart(RaycastHit hit)
    {
        if (_partRenderer != null && _part != null && !_decapitated)
        {
            _decapitated = true;
            _part.gameObject.SetActive(true);
            _part.transform.parent = null;

            _partRenderer.SetActive(false);

            if (_attack != null)
            {
                _attack.enabled = false;
            }

            if (_instantKill)
            {
                AIStateMachine stateMachine = GetComponentInParent <AIStateMachine>();
                if (stateMachine != null)
                {
                    stateMachine.TakeDamage(float.MaxValue, transform.position, hit, false, 1);
                }
            }
        }
    }
コード例 #6
0
        public void Attack(int fromRight = 1)
        {
            // We are gonna attack here by casting a ray in front of the player
            Player player = GoneWrong.Player.instance;

            if (player != null)
            {
                RaycastHit hit;
                Ray        ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
                if (Physics.Raycast(ray, out hit, _hitRange, LayerMask.GetMask("BodyPart")))
                {
                    if (GoneWrong.AudioManager.instance != null && _hitSounds.Count > 0)
                    {
                        AudioClip clip = _hitSounds[Random.Range(0, _hitSounds.Count)];
                        if (clip != null)
                        {
                            GoneWrong.AudioManager.instance.PlayOneShotSound(clip, 1, 0, 1);
                        }
                    }

                    EmitBlood(hit);

                    AIStateMachine stateMachine = hit.transform.GetComponentInParent <AIStateMachine>();
                    if (stateMachine != null)
                    {
                        stateMachine.TakeDamage(_damage, player.transform.position, hit, false, fromRight);

                        // Check if we can decapitate the part
                        AIDecapitation part = hit.transform.GetComponent <AIDecapitation>();
                        if (_decapitator && part != null)
                        {
                            part.DecapitatePart(hit);
                        }
                    }
                }
            }
        }
コード例 #7
0
        public void Fire()
        {
            InventoryWeaponMount weaponMount = null;

            if (_equippedWeapon.value == 0)
            {
                weaponMount = _playerInventory.rifle1;
            }
            else if (_equippedWeapon.value == 1)
            {
                weaponMount = _playerInventory.rifle2;
            }
            else if (_equippedWeapon.value == 2)
            {
                weaponMount = _playerInventory.handgun;
            }
            if (_equippedWeapon.value == 3)
            {
                weaponMount = _playerInventory.melee;
            }

            if (_playerInventory != null && _equippedWeapon != null)
            {
                // If we have rounds left in our weapon, we fire.
                if (weaponMount.rounds > 0)
                {
                    // Muzzle flash activation
                    if (_muzzleFlashTimer <= 0 && _muzzleFlash != null)
                    {
                        _muzzleFlashTimer = _muzzleFlashActiveDelay;
                        _muzzleFlash.gameObject.SetActive(!_muzzleFlash.gameObject.activeSelf);
                    }

                    // Bullet sound
                    if (_bulletSound != null && AudioManager.instance != null && !_gazWeapon)
                    {
                        AudioManager.instance.PlayOneShotSound(_bulletSound, 1, 0, 0);
                    }

                    // Emit Smoke
                    if (_bulletPosition != null)
                    {
                        if (_weaponSmoke != null)
                        {
                            _weaponSmoke.transform.position = _bulletPosition.position;
                            _weaponSmoke.Emit(_smokeEmitted);
                        }

                        if (_impact != null)
                        {
                            _impact.transform.position = _bulletPosition.position;
                            _impact.Emit(10);
                        }

                        if (_multipleImpactObject != null && !_gazWeapon)
                        {
                            _multipleImpactObject.transform.position = _bulletPosition.position;
                            foreach (Transform child in transform)
                            {
                                ParticleSystem childParticleSystem = child.GetComponent <ParticleSystem>();
                                if (childParticleSystem != null)
                                {
                                    childParticleSystem.Emit(10);
                                }
                            }
                        }
                    }

                    // Instantiate a real bullet in the scene (case of a rocket launcher or a heavy weapon with slower ammo speed)
                    if (_bulletPosition != null && _bullet != null)
                    {
                        Bullet bullet = Instantiate(_bullet, _bulletPosition.position, Quaternion.identity);
                        bullet.transform.forward = -transform.forward;
                    }
                    // We make a simple ray cast.
                    else
                    {
                        // If it's a normal fire weapon, we fire a bullet with a raycast
                        if (!_gazWeapon)
                        {
                            // We create a ray
                            Ray        ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
                            RaycastHit hit;
                            if (Physics.Raycast(ray, out hit, Mathf.Infinity, _bulletLayerMask) && _bulletHole != null)
                            {
                                // If we hit an enemy, we are going to provoke damage
                                if (hit.transform.gameObject.layer == LayerMask.NameToLayer("BodyPart"))
                                {
                                    if (GoneWrong.AudioManager.instance != null && _hitSounds.Count > 0)
                                    {
                                        AudioClip clip = _hitSounds[Random.Range(0, _hitSounds.Count)];
                                        if (clip != null)
                                        {
                                            GoneWrong.AudioManager.instance.PlayOneShotSound(clip, 1, 0, 1, hit.transform.position);
                                        }
                                    }

                                    // We emit blood particles at the hit point
                                    EmitBlood(hit);

                                    // Try getting the ai state machine
                                    AIStateMachine stateMachine = hit.transform.GetComponentInParent <AIStateMachine>();
                                    if (stateMachine != null)
                                    {
                                        float damage = _damage;
                                        if (_damageDependsOnDistance && _damages.Count > 0)
                                        {
                                            // By default, the damage should be equal to the least amount of damage
                                            damage = _damages[_damages.Count - 1].damage;

                                            float distance = (stateMachine.transform.position - GoneWrong.Player.instance.transform.position).magnitude;
                                            for (int i = 0; i < _damages.Count; i++)
                                            {
                                                if (distance < _damages[i].distance)
                                                {
                                                    damage = _damages[i].damage;
                                                    break;
                                                }
                                            }
                                        }

                                        // Check if we can decapitate the part
                                        AIDecapitation part = hit.transform.GetComponent <AIDecapitation>();

                                        if (_decapitator && part != null)
                                        {
                                            // Only decapitate the parts that don't cause for an instant kill
                                            if (!part.instantKill || part.CompareTag("Head"))
                                            {
                                                part.DecapitatePart(hit);
                                            }
                                        }

                                        stateMachine.TakeDamage(damage, Player.instance.transform.position, hit);
                                    }
                                }
                                // Else, we mot likely hit default geometry, so we instantiate the bullet hole
                                else
                                {
                                    GameObject tmp = Instantiate(_bulletHole, hit.point, Quaternion.identity);
                                    tmp.transform.forward = hit.normal;
                                    tmp.transform.parent  = hit.transform;
                                }
                            }
                        }
                        else
                        {
                            // This is a gaz weapon
                            // Here, we cast a sphere for the gaz
                            Ray        ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
                            RaycastHit hitInfo;
                            if (Physics.Raycast(ray, out hitInfo, 5f, LayerMask.GetMask("Barrier")))
                            {
                                Barrier barrier = hitInfo.transform.GetComponent <Barrier>();
                                if (barrier != null)
                                {
                                    barrier.TakeDamage(_damage);
                                }
                            }
                        }
                    }

                    // We reduce the number of rounds in our player inventory
                    weaponMount.rounds--;
                }
                else
                {
                    // We play out of ammo sound
                    if (_outOfAmmoSound != null && AudioManager.instance != null)
                    {
                        AudioManager.instance.PlayOneShotSound(_outOfAmmoSound, 1, 0, 0);
                    }

                    // We try to reload.
                    Reload();
                }
            }
        }