1
\$\begingroup\$

<code>[![Something happen like this][1]][1]</code>I made the game space shooter and i want the space fire continuously (auto fire) without click the button. [ This are the code i always click on space bar and its fire but i want its fire continuously (auto fire). if (Input.GetKeyDown (KeyCode.Space) && isEnemyWeapon == false) { Fire (); } ] and i want only player fire continuously not enemy. In my case enemy also fire continuously like laser after taking some times..

Weapon.cs

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

    public Transform laserPrefab ;
    public bool isEnemyWeapon = false;
    public float shootDelay = 0.0f;
    private float waitToShoot;

    void Start () {
        waitToShoot = 0f;
    }

    void Update () {

        if (waitToShoot > 0) {
         waitToShoot = Time.deltaTime;      

        }

    if (Input.GetKeyDown (KeyCode.Space) && isEnemyWeapon == false) {
          Fire ();
    }   

        if (isEnemyWeapon == true && EnemyAttack == false) {
            waitToShoot = shootDelay;
            var shootLaser = Instantiate (laserPrefab) as Transform;
            Vector2 pos = shootLaser.transform.position;
            pos.x = transform.position.x ;
            pos.y = transform.position.y - 0.3f;
            shootLaser.transform.position = pos;
            SoundHelper.instanceSound.EnemyLaserSound();

        }
    }


    public void Fire()
    {
        var shootLaser = Instantiate(laserPrefab) as Transform;
        Vector2 pos = shootLaser.transform.position;
        pos.x = transform.position.x;
        pos.y = transform.position.y + 0.5f;
        shootLaser.transform.position = pos;
        SoundHelper.instanceSound.PlayerLaserSound();


    }

public bool EnemyAttack
    {
         get
        {
         return waitToShoot <= 0f;
        }
    }
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Here's the code which should work for you:

bool firingContinously = false; // is firing right now
float nextFire = 0; // when the next bullet fires
float fireDelay = 100; // 100 milliseconds between each bullet, you can change that

void Update(){
    if(firingContinously && !isEnemyWeapon)
    {
        if(Time.time * 1000 > nextFire){
            nextFire = (Time.time * 1000) + fireDelay; // delay the next fire by the fireDelay
            Fire();
        }
    }
}

In order to fire continously, you change firingContinously to true, and to stop - you change it to false.

I didn't test it. Copy the Update() method contents into your Update() method and these 3 variables somewhere to your class so it looks like:

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

    public Transform laserPrefab ;
    public bool isEnemyWeapon = false;
    public float shootDelay = 0.0f;
    private float waitToShoot;

    public bool firingContinously = false;
    float nextFire = 0;
    float fireDelay = 100; // 100 milliseconds between each bullet


    void Start () {
        waitToShoot = 0f;
    }

    void Update () {


        if(firingContinously && !isEnemyWeapon)
        {
            if(Time.time * 1000 > nextFire){
                nextFire = (Time.time * 1000) + fireDelay;
                Fire();
            }
        }


        if (waitToShoot > 0) {
         waitToShoot = Time.deltaTime;      

        }

        if (Input.GetKeyDown (KeyCode.Space) && isEnemyWeapon == false) {
              Fire ();
        }   

        if (isEnemyWeapon == true && EnemyAttack == false) {
            waitToShoot = shootDelay;
            var shootLaser = Instantiate (laserPrefab) as Transform;
            Vector2 pos = shootLaser.transform.position;
            pos.x = transform.position.x ;
            pos.y = transform.position.y - 0.3f;
            shootLaser.transform.position = pos;
            SoundHelper.instanceSound.EnemyLaserSound();

        }
    }


    public void Fire()
    {
        var shootLaser = Instantiate(laserPrefab) as Transform;
        Vector2 pos = shootLaser.transform.position;
        pos.x = transform.position.x;
        pos.y = transform.position.y + 0.5f;
        shootLaser.transform.position = pos;
        SoundHelper.instanceSound.PlayerLaserSound();


    }

public bool EnemyAttack
    {
         get
        {
         return waitToShoot <= 0f;
        }
    }
}
\$\endgroup\$
11
  • \$\begingroup\$ Hello Spectre, I applied your code and i test it but the enemy and player both are not firing now. \$\endgroup\$ Aug 11, 2017 at 8:33
  • \$\begingroup\$ I'll be home and fix it in 10-15 minutes :) \$\endgroup\$
    – Jacob
    Aug 11, 2017 at 8:43
  • \$\begingroup\$ nextFire += fireDelay; Here's an error (mine). We want to set the delay to currentTime + fireDelay. So it should be: nextFire = (Time.time * 1000) + fireDelay; I'll edit the code. You try it out. \$\endgroup\$
    – Jacob
    Aug 11, 2017 at 8:58
  • \$\begingroup\$ I am able to fire now but continuously fire not working (for fire still i pressed the space_bar). \$\endgroup\$ Aug 11, 2017 at 9:07
  • \$\begingroup\$ That's why you have a firingContinously variable. Change it to true and check what happens :) \$\endgroup\$
    – Jacob
    Aug 11, 2017 at 9:08
0
\$\begingroup\$

First, you should not have one class for both your enemy weapon and your player weapon. Create a separate class for each of them. And having a base class could be a good idea too :

using UnityEngine;
using System.Collections;

public abstract class Weapon : MonoBehaviour {

    public Transform laserPrefab ;
    public float shootDelay = 0.1f;
    protected float nextFireTime;

    public abstract bool CanShoot
    {
        get;
    }

    void Start ()
    {
        nextFireTime = Time.time;
    }

    void Update ()
    {
        if ( CanShoot )
        {
            Fire ();
        }
    }

    public virtual void Fire()
    {
        nextFireTime = Time.time + shootDelay;
    }
}

using UnityEngine;
using System.Collections;

public class PlayerWeapon : Weapon {

    public override bool CanShoot
    {
        get { return Input.GetKey(KeyCode.Space) && Time.time >= nextFireTime  ; }
    }

    public override void Fire()
    {
        base.Fire();
        var shootLaser = Instantiate(laserPrefab) as Transform;
        Vector2 pos = shootLaser.transform.position;
        pos.x = transform.position.x;
        pos.y = transform.position.y + 0.5f;
        shootLaser.transform.position = pos;
        SoundHelper.instanceSound.PlayerLaserSound();
    }
}

using UnityEngine;
using System.Collections;

public class EnemyWeapon : Weapon {

    public override bool CanShoot
    {
        get { return Time.time >= nextFireTime ; }
    }

    public override void Fire()
    {
        base.Fire();
        var shootLaser = Instantiate (laserPrefab) as Transform;
        Vector2 pos = shootLaser.transform.position;
        pos.x = transform.position.x ;
        pos.y = transform.position.y - 0.3f;
        shootLaser.transform.position = pos;
        SoundHelper.instanceSound.EnemyLaserSound();
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Sorry Hellium, i try your code too but its not work!! \$\endgroup\$ Aug 11, 2017 at 9:41
  • \$\begingroup\$ What does not work ? You have to create 3 scripts : Weapon, PlayerWeapon and EnemyWeapon. Attach PlayerWeapon to your player gameobject, EnemyWeapon to your enemy and specify the correct values for laserPrefab and shootDelay. \$\endgroup\$
    – Hellium
    Aug 11, 2017 at 10:37
  • \$\begingroup\$ I made an edit an hour ago in the CanShoot property, make sure you have the correct one. And change the protection level of nextFireTime \$\endgroup\$
    – Hellium
    Aug 11, 2017 at 10:44
  • \$\begingroup\$ I just have tested the scripts I gave you, it works fine. \$\endgroup\$
    – Hellium
    Aug 11, 2017 at 10:54
  • \$\begingroup\$ My problem is solved, thank you so much Hellium for helping me :) \$\endgroup\$ Aug 11, 2017 at 10:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .