// Update is called once per frame
    void Update()
    {
        if (Time.time > lastFireTime + muzzleFlashTime)
        {
            muzzleFlashFront.SetActive(false);
        }

        if (firing)
        {
            if (Time.time > lastFireTime + 1 / frequency)
            {
                if (quiver.GetCurrentWeaponAmmoCount() > 0)
                {
                    if (audio)
                    {
                        audio.Play();
                    }

                    muzzleFlashFront.SetActive(true);

                    var          coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0);
                    GameObject   go     = Spawner.Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
                    SimpleBullet bullet = go.GetComponent <SimpleBullet>();

                    lastFireTime = Time.time;

                    quiver.ShootCurrentWeapon();;
                }
            }
        }
    }
Exemple #2
0
    IEnumerator GenerateSandanBullets(Weapon currentWeapon, GameObject bulletObj, Transform spawnPoint)
    {
        for (int i = 0; i < 4; i++)
        {
            float      xOffset        = Random.Range(-15f, 15f);
            float      yOffset        = Random.Range(-15f, 15f);
            float      duration       = Random.Range(0.01f, 0.05f);
            GameObject bulletObjRight = null;
            if (!automatic)
            {
                bulletObjRight = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar);
            }
            else
            {
                bulletObjRight = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar);
            }
            bulletObjRight.transform.parent   = bulletsArea;
            bulletObjRight.transform.position = spawnPoint.position;
            bulletObjRight.transform.rotation = Quaternion.Euler(new Vector3(xOffset, yOffset, 0)) * spawnPoint.rotation;
            SimpleBullet rightbullet = bulletObjRight.GetComponent <SimpleBullet>();
            rightbullet.distance = currentWeapon.range;
            rightbullet.damage   = currentWeapon.atk;
            rightbullet.InitWithSpawnPoint(spawnPoint.gameObject, Quaternion.Euler(new Vector3(xOffset, yOffset, 0)));
            yield return(new WaitForSeconds(duration));
        }


        SimpleBullet midbullet = bulletObj.GetComponent <SimpleBullet>();

        midbullet.distance = currentWeapon.range;
        midbullet.damage   = currentWeapon.atk;
        midbullet.InitWithSpawnPoint(spawnPoint.gameObject, Quaternion.Euler(Vector3.zero));
        yield return(new WaitForSeconds(0.01f));
    }
Exemple #3
0
 public override void Update()
 {
     if (this.firing && (Time.time > (this.lastFireTime + (((float)1) / this.frequency))))
     {
         Quaternion   quaternion = Quaternion.Euler(UnityEngine.Random.Range(-this.coneAngle, this.coneAngle), UnityEngine.Random.Range(-this.coneAngle, this.coneAngle), (float)0);
         SimpleBullet component  = (Spawner.Spawn(this.bulletPrefab, this.spawnPoint.position, this.spawnPoint.rotation * quaternion) as GameObject).GetComponent <SimpleBullet>();
         this.lastFireTime = Time.time;
         RaycastHit hitInfo = this.raycast.GetHitInfo();
         if (hitInfo.transform != null)
         {
             Health health = hitInfo.transform.GetComponent <Health>();
             if (health != null)
             {
                 health.OnDamage(this.damagePerSecond / this.frequency, -this.spawnPoint.forward);
             }
             if (hitInfo.rigidbody != null)
             {
                 Vector3 force = (Vector3)(this.transform.forward * (this.forcePerSecond / this.frequency));
                 hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
             }
             component.dist = hitInfo.distance;
         }
         else
         {
             component.dist = 0x3e8;
         }
     }
 }
Exemple #4
0
 private void Start()
 {
     //gameObject.SetActive(false)
     mainplayer = GameObject.FindGameObjectWithTag("Player");
     //Needed in order to get the power value of the bullet that we are using
     playbull = playerBullet.GetComponent <SimpleBullet>();
 }
    // Update is called once per frame
    void Update()
    {
        if (Time.time > lastFireTime + muzzleFlashTime)
        {
            muzzleFlashFront.SetActive(false);
        }

        if (firing && Time.time > lastFireTime + 1 / frequency && quiver.GetCurrentWeaponAmmoCount() > 0)
        {
            if (audio)
            {
                audio.Play();
            }

            muzzleFlashFront.SetActive(true);

            // Spawn visual bullet
            var          coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0);
            GameObject   go     = Spawner.Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
            SimpleBullet bullet = go.GetComponent <SimpleBullet>();

            lastFireTime = Time.time;

            quiver.ShootCurrentWeapon();

            // Find the object hit by the raycast
            RaycastHit hitInfo = raycast.GetHitInfo();
            if (hitInfo.transform)
            {
                // Get the health component of the target if any
                Health targetHealth = hitInfo.transform.GetComponent <Health>();
                if (targetHealth)
                {
                    // Apply damage
                    targetHealth.OnDamage(damagePerSecond / frequency, -spawnPoint.forward);
                }

                // Get the rigidbody if any
                if (hitInfo.rigidbody)
                {
                    // Apply force to the target object at the position of the hit point
                    Vector3 force = transform.forward * (forcePerSecond / frequency);
                    hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
                }

                bullet.dist = hitInfo.distance;
            }
            else
            {
                bullet.dist = 1000;
            }
            Debug.Log("bullet distance: " + bullet.dist);
        }
        else
        {
            muzzleFlashFront.SetActive(false);
        }
    }
Exemple #6
0
    // Update is called once per frame
    void Update()
    {
        // left mouse button click
        if (playerScript)
        {
            if (Input.GetButtonDown("Fire1") | Input.GetButton("Fire1"))
            {
                if (Time.time > lastFireTime + 1 / frequency)
                {
                    // Spawn visual bullet

                    //Quaternion tempRot =  Quaternion.LookRotation(playerScript.GetMouseOnPlane() - muzzlePosition.position , Vector3.forward);
                    //tempRot.z = 0f;
                    //tempRot.w = 0.5f;

                    Quaternion tempRot = transform.parent.transform.rotation;

                    Quaternion   coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0);
                    GameObject   go     = (GameObject)Instantiate(bulletPrefab, muzzlePosition.position, tempRot * coneRandomRotation);
                    SimpleBullet bullet = go.GetComponent <SimpleBullet> ();

                    muzzleParticle.Emit(1);

                    lastFireTime = Time.time;

                    // Find the object hit by the raycast
                    RaycastHit hitInfo = new RaycastHit();
                    Physics.Raycast(transform.position, transform.parent.forward, out hitInfo, 100);

                    if (hitInfo.transform)
                    {
                        // Get the health component of the target if any
                        HealthHandler targetHealth = hitInfo.transform.GetComponent <HealthHandler> ();

                        if (targetHealth & (hitInfo.transform.root != transform.root))
                        {
                            // Apply damage
                            targetHealth.DeductHealth(Mathf.FloorToInt(damagePerSecond / frequency));
                        }

                        // Get the rigidbody if any
                        if (hitInfo.rigidbody)
                        {
                            // Apply force to the target object at the position of the hit point
                            Vector3 force = transform.forward * (forcePerSecond / frequency);
                            hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
                        }
                        bullet.dist = hitInfo.distance;
                    }
                    else
                    {
                        bullet.dist = 1000;
                    }
                }
            }
        }
    }
Exemple #7
0
 public void Fire()
 {
     if (timeSinceLastShot >= Cooldown)
     {
         SimpleBullet clonedBullet = Instantiate(bullet, transform);
         clonedBullet.Init(damage, speed, _playerEngine.Position + new Vector2(-0.5f, 0));
         timeSinceLastShot = 0;
     }
 }
Exemple #8
0
 public void Fire()
 {
     if (_playerAvatar.Energy > 0 && timeSinceLastShot >= Cooldown)
     {
         SimpleBullet clonedBullet = Instantiate(bullet, transform);
         clonedBullet.Init(damage, speed, _playerEngine.Position + new Vector2(1, 0));
         timeSinceLastShot = 0;
         _playerAvatar.Energy--;
     }
 }
Exemple #9
0
    private void TryFire()
    {
        timeSinceLastFire += Time.deltaTime * GameManager.timeScale;
        if (timeSinceLastFire < fireInterval)
        {
            return;
        }
        timeSinceLastFire = 0;
        float        _dir    = curAngle + Random.Range(-sprayAngle, sprayAngle);
        SimpleBullet _bullet = Instantiate(bulletGO, this.transform.position + this.transform.up * 3.5f, Quaternion.Euler(Vector3.forward * _dir), null).GetComponent <SimpleBullet>();

        _bullet.velocity = AlterVector(Vector3.up * bulletSpeed, _dir);
    }
Exemple #10
0
    IEnumerator Shoot()
    {
        if (canShoot)
        {
            canShoot = false;
            SimpleBullet bullet = ObjectQueue.Instance.SpawnFromPool("Bullet", transform.position, transform.rotation).GetComponent <SimpleBullet>();
            yield return(new WaitForSeconds(fireRate));

            canShoot = true;
        }

        yield return(null);
    }
Exemple #11
0
    private void OnCollisionEnter2D(Collision2D other)
    {
        Rigidbody2D otherRB = other.gameObject.GetComponent <Rigidbody2D>();

        if (otherRB)
        {
            if (other.gameObject.CompareTag("PlayerBullet") || other.gameObject.CompareTag("EnemyBullet"))
            {
                SimpleBullet bullet = other.gameObject.GetComponent <SimpleBullet>();
                Impact(Random.Range(amplitude / 2, amplitude), bullet.velocity.normalized);
            }
        }
    }
Exemple #12
0
    private void fireBullet(RaycastHit hit, bool rigidbody)
    {
        GameObject obj = Instantiate(bullet[bulletType], shotSpawn.position, shotSpawn.rotation) as GameObject;

        SimpleBullet simpleBullet = obj.GetComponent <SimpleBullet> ();

        if (rigidbody)
        {
            simpleBullet.enableRigidbody(hit);
        }
        else
        {
            simpleBullet.enable(hit);
        }
        // or enable
        //	simpleBullet.enableRigidbody (hit);
    }
Exemple #13
0
    void Update()
    {
        if (firing)
        {
            if (Time.time > lastFireTime + 1 / frequency)
            {
                // Spawn visual bullet
                Quaternion   coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0);
                GameObject   go     = Spawner.Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
                SimpleBullet bullet = go.GetComponent <SimpleBullet> ();

                lastFireTime = Time.time;

                // Find the object hit by the raycast
                RaycastHit hitInfo = raycast.GetHitInfo();
                if (hitInfo.transform)
                {
                    // Get the health component of the target if any
                    Health targetHealth = hitInfo.transform.GetComponent <Health> ();
                    if (targetHealth)
                    {
                        // Apply damage
                        targetHealth.OnDamage(damagePerSecond / frequency, -spawnPoint.forward);
                    }

                    // Get the rigidbody if any
                    if (hitInfo.rigidbody)
                    {
                        // Apply force to the target object at the position of the hit point
                        Vector3 force = transform.forward * (forcePerSecond / frequency);
                        hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
                    }

                    // Ricochet sound
                    AudioClip sound = MaterialImpactManager.GetBulletHitSound(hitInfo.collider.sharedMaterial);
                    AudioSource.PlayClipAtPoint(sound, hitInfo.point, hitSoundVolume);

                    bullet.dist = hitInfo.distance;
                }
                else
                {
                    bullet.dist = 1000;
                }
            }
        }
    }
    public void NewBullet(Vector3 position, Vector3 direction)
    {
        direction = direction.normalized;
        Rigidbody bullet;

        if (!TryTakeRBFromInactivePool(out bullet))
        {
            bulletCount++;
            GameObject bulletObject = Instantiate(bulletPrefab) as GameObject;
            bulletObject.transform.parent = this.transform;
            bulletObject.name             = "bullet " + bulletCount;
            SimpleBullet bulletScript = bulletObject.GetComponent <SimpleBullet>();
            bulletScript.damage     = bulletDamage;
            bulletScript.pool       = this;
            bulletScript.effectPool = effectPool;
            bullet = bulletObject.GetComponent <Rigidbody>();
        }
        bullet.gameObject.SetActive(true);
        bullet.transform.position      = position;
        bullet.transform.localRotation = Quaternion.LookRotation(direction);
        bullet.velocity = direction * bulletSpeed;
        activeRBs.Add(bullet);
    }
Exemple #15
0
    IEnumerator ShootBullet()
    {
        Weapon currentWeapon = WeaponController.Instance.GetWeaponByID(currentWeaponId);

        //		if (automatic)
        //			currentWeapon = GetComponent<FakeWeaponController>().currentWeapon;

        if (currentWeapon == null)
        {
            Debug.LogError("can't find the weapon by id " + currentWeaponId);
            yield break;
        }

        if (currentWeaponId == WeaponType.dianju)
        {
            yield break;
        }

        //		if (Time.time > lastFireTime + 1f / currentWeapon.Frequency)
        {
            lastFireTime = Time.time;
            GameObject bulletObj = null;
            if (!automatic)
            {
                bulletObj = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar);
            }
            else
            {
                bulletObj = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar);
            }

            //			bulletObj.transform.parent = bulletsArea;
            bulletObj.transform.position = spawnPointR.position - spawnPointR.transform.forward * 4.38f;
            bulletObj.transform.rotation = spawnPointR.rotation;

            GameObject qiangkou  = null;
            GameObject qiangkou2 = null;
            if (currentWeapon.ID == WeaponType.gun_sandan || currentWeapon.ID == WeaponType.gundouble_sandan)
            {
                qiangkou = (GameObject)Instantiate(Resources.Load("prefabs/Effects/gun_sandan"));
                if (currentWeapon.ID == WeaponType.gundouble_sandan)
                {
                    qiangkou2 = (GameObject)Instantiate(Resources.Load("prefabs/Effects/gun_sandan"));
                }
            }
            else
            {
                qiangkou = (GameObject)Instantiate(Resources.Load("prefabs/bullets/qiangkou"));
                if (currentWeapon.ID > WeaponType.gun_single && currentWeapon.ID < WeaponType.gun_double)
                {
                    qiangkou2 = (GameObject)Instantiate(Resources.Load("prefabs/bullets/qiangkou2"));
                }
            }

            qiangkou.transform.parent   = bulletsArea;
            qiangkou.transform.position = spawnPointR.transform.position + new Vector3(0.5f, 0, 0);
            if (qiangkou2 != null)
            {
                qiangkou2.transform.parent   = bulletsArea;
                qiangkou2.transform.position = spawnPointL.transform.position + new Vector3(0.5f, 0, 0);
            }

            if (currentWeaponId == WeaponType.gun_liudan || currentWeaponId == WeaponType.gundouble_liudan)
            {
                Ultilities.gm.audioScript.liudanFX.play();
                LiudanBullet liudanBullet = bulletObj.GetComponent <LiudanBullet>();
                liudanBullet.distance = currentWeapon.range;
                liudanBullet.damage   = currentWeapon.atk;
                //						liudanBullet.distance = currentWeapon.Distance;
                liudanBullet.InitWithSpawnPoint(spawnPointR.gameObject);
                if (!automatic)
                {
                    WeaponController.Instance.DecreaseWeaponCapacity();
                }

                if (currentWeaponId == WeaponType.gundouble_liudan)
                {
                    yield return(new WaitForSeconds(0.5f));

                    Ultilities.gm.audioScript.liudanFX.play();
                    GameObject bulletObj2 = null;
                    if (!automatic)
                    {
                        bulletObj2 = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar2);
                    }
                    else
                    {
                        bulletObj2 = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar2);
                    }

                    //					bulletObj2.transform.parent = bulletsArea;
                    bulletObj2.transform.position = spawnPointL.position - spawnPointL.transform.forward * 2.88f;
                    bulletObj2.transform.rotation = spawnPointL.rotation;
                    LiudanBullet liudanBullet2 = bulletObj2.GetComponent <LiudanBullet>();
                    liudanBullet2.distance = currentWeapon.range;
                    liudanBullet2.damage   = currentWeapon.atk;
                    //						liudanBullet.distance = currentWeapon.Distance;
                    liudanBullet2.InitWithSpawnPoint(spawnPointL.gameObject);

                    if (!automatic)
                    {
                        WeaponController.Instance.DecreaseWeaponCapacity();
                    }
                }
            }
            else if (currentWeaponId == WeaponType.gun_sandan || currentWeaponId == WeaponType.gundouble_sandan)
            {
                GenerateZidanke();
                Ultilities.gm.audioScript.sandanFX.play();
                GameObject zidanRightEffect = (GameObject)Instantiate(Resources.Load("prefabs/Bullets/zidan_sandan"));
                zidanRightEffect.transform.position = spawnPointR.position;
                zidanRightEffect.transform.rotation = transform.rotation;
                for (int i = 0; i < 4; i++)
                {
                    float xOffset = Random.Range(-10f, 10f);
                    float yOffset = Random.Range(-10f, 10f);

                    //							float duration = Random.Range(0.01f, 0.05f);
                    GameObject bulletObjRight = null;
                    if (!automatic)
                    {
                        bulletObjRight = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar);
                    }
                    else
                    {
                        bulletObjRight = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar);
                    }


                    //					bulletObjRight.transform.parent = bulletsArea;
                    bulletObjRight.transform.position = spawnPointR.position;
                    bulletObjRight.transform.rotation = Quaternion.Euler(new Vector3(xOffset, yOffset, 0)) * spawnPointR.rotation;
                    SimpleBullet rightbullet = bulletObjRight.GetComponent <SimpleBullet>();
                    rightbullet.distance = currentWeapon.range;
                    rightbullet.damage   = currentWeapon.atk;
                    rightbullet.InitWithSpawnPoint(spawnPointR.gameObject, Quaternion.Euler(new Vector3(xOffset, yOffset, 0)));
                }


                //				SimpleBullet midbullet = bulletObj.GetComponent<SimpleBullet>();
                //				midbullet.distance = currentWeapon.range;
                //				midbullet.damage = currentWeapon.atk;
                //				midbullet.InitWithSpawnPoint(spawnPointR.gameObject, Quaternion.Euler(Vector3.zero));
                //						yield return new WaitForSeconds(0.01f);
                if (!automatic)
                {
                    WeaponController.Instance.DecreaseWeaponCapacity();
                }

                if (currentWeaponId == WeaponType.gundouble_sandan)
                {
                    yield return(new WaitForSeconds(0.3f));

                    Ultilities.gm.audioScript.sandanFX.play();
                    GameObject zidanLeftEffect = (GameObject)Instantiate(Resources.Load("prefabs/Bullets/zidan_sandan"));
                    zidanLeftEffect.transform.position = spawnPointL.position;
                    zidanLeftEffect.transform.rotation = transform.rotation;
                    for (int i = 0; i < 4; i++)
                    {
                        float xOffset = Random.Range(-10f, 10f);
                        float yOffset = Random.Range(-10f, 10f);
                        //							float duration = Random.Range(0.01f, 0.05f);
                        GameObject bulletObjLeft = null;
                        if (!automatic)
                        {
                            bulletObjLeft = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar2);
                        }
                        else
                        {
                            bulletObjLeft = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar2);
                        }
                        //						bulletObjLeft.transform.parent = bulletsArea;
                        bulletObjLeft.transform.position = spawnPointL.position;
                        bulletObjLeft.transform.rotation = Quaternion.Euler(new Vector3(xOffset, yOffset, 0)) * spawnPointL.rotation;
                        SimpleBullet rightbullet = bulletObjLeft.GetComponent <SimpleBullet>();
                        rightbullet.distance = currentWeapon.range;
                        rightbullet.damage   = currentWeapon.atk;
                        rightbullet.InitWithSpawnPoint(spawnPointL.gameObject, Quaternion.Euler(new Vector3(xOffset, yOffset, 0)));
                    }


                    //					SimpleBullet midbullet2 = bulletObj.GetComponent<SimpleBullet>();
                    //					midbullet2.distance = currentWeapon.range;
                    //					midbullet2.damage = currentWeapon.atk;
                    //					midbullet2.InitWithSpawnPoint(spawnPointL.gameObject, Quaternion.Euler(Vector3.zero));
                    //						yield return new WaitForSeconds(0.01f);
                    if (!automatic)
                    {
                        WeaponController.Instance.DecreaseWeaponCapacity();
                    }
                }
            }
            else if (currentWeaponId == WeaponType.gun_fire || currentWeaponId == WeaponType.gundouble_fire)
            {
            }
            else
            {
                if (currentWeaponId == WeaponType.gun_shouqiang)
                {
                    Ultilities.gm.audioScript.shouqiangSoundFX.play();
                }
                else if (currentWeaponId == WeaponType.gundouble_shouqiang)
                {
                    Ultilities.gm.audioScript.shouqiangSoundFX.play();
                    Ultilities.gm.audioScript.shouqiangSoundFX.play();
                }
                else if (currentWeaponId == WeaponType.gun_m4 || currentWeaponId == WeaponType.gun_jiatelin)
                {
                    Ultilities.gm.audioScript.jiqiangSoundFX.play();
                }
                else if (currentWeaponId == WeaponType.gundouble_m4)
                {
                    Ultilities.gm.audioScript.jiqiangSoundFX.play();
                    Ultilities.gm.audioScript.jiqiangSoundFX.play();
                }

                GenerateZidanke();
                SimpleBullet bullet = bulletObj.GetComponent <SimpleBullet>();
                bullet.distance = currentWeapon.range;
                bullet.damage   = currentWeapon.atk;
                bullet.InitWithSpawnPoint(spawnPointR.gameObject, Quaternion.Euler(Vector3.zero));
                if (!automatic)
                {
                    WeaponController.Instance.DecreaseWeaponCapacity();
                }

                if (currentWeaponId > WeaponType.gun_single && currentWeaponId < WeaponType.gun_double)
                {
                    yield return(new WaitForSeconds(0.05f));

                    GameObject bulletObj2 = null;
                    if (!automatic)
                    {
                        bulletObj2 = (GameObject)Instantiate(WeaponController.Instance.GetWeaponByID(currentWeaponId).BulletAvatar2);
                    }
                    else
                    {
                        bulletObj2 = (GameObject)Instantiate(GetComponent <FakeWeaponController>().currentWeapon.BulletAvatar2);
                    }
                    //					bulletObj2.transform.parent = bulletsArea;
                    bulletObj2.transform.position = spawnPointL.position - spawnPointL.transform.forward * 4.88f;
                    bulletObj2.transform.rotation = spawnPointL.rotation;
                    SimpleBullet bullet2 = bulletObj2.GetComponent <SimpleBullet>();
                    bullet2.distance = currentWeapon.range;
                    bullet2.damage   = currentWeapon.atk;
                    bullet2.InitWithSpawnPoint(spawnPointL.gameObject, Quaternion.Euler(Vector3.zero));
                    if (!automatic)
                    {
                        WeaponController.Instance.DecreaseWeaponCapacity();
                    }
                }
            }
        }

        yield return(null);
    }
Exemple #16
0
    void  Update()
    {
        if (false)
        {
            float axisX = Input.GetAxis("RightHorizontal");
            float axisY = Input.GetAxis("RightVertical");

            if (Input.GetButtonDown("Joystick button 15") || (!firing && false && Mathf.Abs(axisX) + Mathf.Abs(axisY) > 0.0f))
            {
                if (Time.timeScale == 0)
                {
                    return;
                }

                firing = true;

                muzzleFlashFront.SetActive(true);

                if (asource)
                {
                    asource.Play();
                }
            }

            if (Input.GetButtonUp("Joystick button 15") || (firing && false && Mathf.Abs(axisX) + Mathf.Abs(axisY) < 0.01f))
            {
                firing = false;

                muzzleFlashFront.SetActive(false);

                if (asource)
                {
                    asource.Stop();
                }
            }
        }

        if (firing)
        {
            if (Time.time > lastFireTime + 1 / frequency)
            {
                // Spawn visual bullet
                Quaternion   coneRandomRotation = Quaternion.Euler(Random.Range(-coneAngle, coneAngle), Random.Range(-coneAngle, coneAngle), 0);
                GameObject   go     = Spawner.Spawn(bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
                SimpleBullet bullet = go.GetComponent <SimpleBullet> ();

                lastFireTime = Time.time;

                // Find the object hit by the raycast
                RaycastHit hitInfo = raycast.GetHitInfo();
                if (hitInfo.transform)
                {
                    // Get the health component of the target if any
                    Health targetHealth = hitInfo.transform.GetComponent <Health> ();
                    if (targetHealth)
                    {
                        // Apply damage
                        targetHealth.OnDamage(damagePerSecond / frequency, -spawnPoint.forward);
                    }

                    // Get the rigidbody if any
                    if (hitInfo.rigidbody)
                    {
                        // Apply force to the target object at the position of the hit point
                        Vector3 force = transform.forward * (forcePerSecond / frequency);
                        hitInfo.rigidbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
                    }

                    // Ricochet sound
                    //AudioClip sound = MaterialImpactManager.GetBulletHitSound (hitInfo.collider.sharedMaterial);
                    //AudioSource.PlayClipAtPoint (sound, hitInfo.point, hitSoundVolume);

                    bullet.dist = hitInfo.distance;
                }
                else
                {
                    bullet.dist = 1000;
                }
            }
        }
    }
Exemple #17
0
 // Start is called before the first frame update
 void Start()
 {
     lineRenderer   = GetComponent <LineRenderer>();
     spriteRenderer = GetComponent <SpriteRenderer>();
     simpleBullet   = GetComponent <SimpleBullet>();
 }
Exemple #18
0
    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR || UNITY_STANDALONE
        if (Input.GetKeyDown(KeyCode.F))
        {
            fire = true;
            EventService.Instance.GetEvent <FireChangeEvent>().Publish(true);
        }
        else if (Input.GetKeyUp(KeyCode.O))
        {
            fire = false;
            EventService.Instance.GetEvent <FireChangeEvent>().Publish(false);
        }
#endif

#if UNITY_IPHONE || UNITY_ANDROID
        if (btnFire.IsPressed())
        {
            if (Time.timeScale == 0)
            {
                return;
            }

            fire = true;
            EventService.Instance.GetEvent <FireChangeEvent>().Publish(true);
        }
        else
        {
            fire = false;
            EventService.Instance.GetEvent <FireChangeEvent>().Publish(false);
        }
#endif

        if (fire)
        {
            if (Time.time > lastFireTime + 1f / frequency)
            {
                lastFireTime = Time.time;
                GameObject   bulletObj = (GameObject)Instantiate(bulletPrefab, spawnPoint.position, spawnPoint.rotation);
                SimpleBullet bullet    = bulletObj.GetComponent <SimpleBullet>();

                Quaternion q       = Quaternion.Euler(Vector3.zero);
                RaycastHit hitinfo = transform.GetComponent <BulletRaycast>().GetHitInfo(q);

                if (hitinfo.transform != null && hitinfo.distance <= bullet.distance)
                {
                    if (hitinfo.transform.tag == "Enemy")
                    {
                        //hit the enemy
                        Vector3 force = transform.forward * 10 / frequency;
                        hitinfo.rigidbody.AddForce(force, ForceMode.Impulse);
//						bullet.distance = hitinfo.distance;


                        EventService.Instance.GetEvent <EnemyStateChangeEvent>().Publish(AnimState.take1, hitinfo.transform.GetComponent <EnemyController>().EnemyId);
                        hitinfo.transform.GetComponent <EnemyController>().onDamage(bullet.damage, hitinfo.point);
                    }
                    else
                    {
                        Debug.Log("hitinfo's name is " + hitinfo.transform.gameObject.name);
                    }

                    bullet.distance = hitinfo.distance;
                }
            }
        }
    }