Beispiel #1
0
        /// <summary>
        /// Fires the weapon
        /// </summary>
        /// <returns></returns>
        public bool FireWeapon()
        {
            // If the weapon is currently not recovering from its fire rate
            if (!Fired)
            {
                // Get the texture of the gun
                Texture2D gunTexture = assets.GetWeaponTexture(Weapon);

                // Set the emit direction to be the rotation of the weapon plus the
                // weapons width (in front of the gun)
                Vector2 direction = new Vector2(
                    (float)Math.Cos(Rotation) * gunTexture.Width,
                    (float)Math.Sin(Rotation) * gunTexture.Width);

                // If the weapon is not a knife
                if (Weapon != WeaponData.Weapon.Knife)
                {
                    // Launch the particle
                    gunSmokeParticle.Launch(direction + Position, Rotation);

                    // Launch the shell
                    shellParticle.Launch(new Vector2(Position.X + direction.X / 2f, Position.Y + direction.Y),
                                         Rotation + 1.57f);
                }

                // Set weapon to fired until cooldown
                Fired    = true;
                FireRate = WeaponData.FireRate(Weapon);
            }
            return(Fired);
        }
    private void Shoot(float deltaTime)
    {
        if (angleToPlayer > angleThreshold || toPlayer.magnitude > distanceThreshold)
        {
            return;
        }

        // Start shooting at player if in view (ignoring projectile layer, 9)
        int        layerMask = ~(1 << 9);
        RaycastHit hit;

        if (Physics.Raycast(projectileSpawnPoint.transform.position, toPlayer, out hit, Mathf.Infinity, layerMask))
        {
            projectileSpawnTimer += deltaTime;
            if (hit.transform.name == "Player" && projectileSpawnTimer >= 1f / weapon.FireRate())
            {
                // Spawn projectile
                ProjectilePooler.Instance.SpawnProjectile(weapon.ProjectileTag(),
                                                          projectileSpawnPoint.transform.position, projectileSpawnPoint.transform.rotation);
                projectileSpawnTimer = 0f;

                // Spawn muzzle flash particle
                GameObject muzzleFlash = Instantiate(weapon.MuzzleFlashPrefab(),
                                                     projectileSpawnPoint.transform.position, projectileSpawnPoint.transform.rotation);
                muzzleFlash.transform.SetParent(projectileSpawnPoint.transform);
            }
        }
    }
    private void Shoot(float deltaTime)
    {
        // Override look direction of movement with shoot direction
        bool isShooting = SetLookDirection(shootDirection);

        if (isShooting)
        {
            projectileSpawnTimer += deltaTime;

            if (projectileSpawnTimer >= 1f / weapon.FireRate())
            {
                // Spawn projectile
                ProjectilePooler.Instance.SpawnProjectile(weapon.ProjectileTag(),
                                                          projectileSpawnPoint.transform.position, rb.rotation);
                projectileSpawnTimer = 0f;

                // Spawn muzzle flash particle
                GameObject muzzleFlash = Instantiate(weapon.MuzzleFlashPrefab(),
                                                     projectileSpawnPoint.transform.position, rb.rotation);
                muzzleFlash.transform.SetParent(projectileSpawnPoint.transform);
            }
        }
        else
        {
            projectileSpawnTimer = 0f;
        }
    }
Beispiel #4
0
    private void Shoot(float deltaTime, Vector3 shootDirection)
    {
        navMeshAgent.transform.rotation = Quaternion.LookRotation(shootDirection, Vector3.up);

        projectileSpawnTimer += deltaTime;

        // Only shoot if aiming at player (ignore projectile layer, 9)
        int        layerMask = ~(1 << 9);
        RaycastHit hit;

        if (Physics.Raycast(projectileSpawnPoint.transform.position, projectileSpawnPoint.transform.forward, out hit, Mathf.Infinity, layerMask))
        {
            if (hit.transform.name == "Player" && projectileSpawnTimer >= 1f / weapon.FireRate())
            {
                // Spawn projectile
                ProjectilePooler.Instance.SpawnProjectile(weapon.ProjectileTag(),
                                                          projectileSpawnPoint.transform.position, navMeshAgent.transform.rotation);
                projectileSpawnTimer = 0f;

                // Spawn muzzle flash particle
                GameObject muzzleFlash = Instantiate(weapon.MuzzleFlashPrefab(),
                                                     projectileSpawnPoint.transform.position, navMeshAgent.transform.rotation);
                muzzleFlash.transform.SetParent(projectileSpawnPoint.transform);
            }
        }
    }