0
\$\begingroup\$

I want some limits or range when my player fire the bullets and when i press the shoot button my fire as been stopped. I use the GetButtonDown from this its fire continuously but its not stopped. and if i use Input.GetButton than its hold the button and fire continuously but in fire there is no gap. So, my problem is that how i stop the bullet fire when i press the shoot button and if choose this (INPUT.GETBUTTON) and its hold the shoot button and its fire continuously than i want some gap on that fire and some limits or range of fire.

void Update(){
        if(Input.GetButtonDown("Fire1"))
        {
            shootRoutine = StartCoroutine(PlayerShoot());


        }else if(Input.GetButtonUp("JumpButton"))
        {
            StopCoroutine(shootRoutine);
            shootRoutine = null;

        }
    }

    void JumpButton(){
        if(!transform.GetChild(0).gameObject.GetComponent<PlayerHealth>
            ().hasDied){
            if (grounded){
                grounded = false;
                myRigidBody.AddForce (new Vector2 (0, 
                    jumpPower), ForceMode2D.Impulse);
                if(myRigidBody.velocity.y > jumpPower){
                    myRigidBody.velocity = new Vector2 (0, 
                        jumpPower);
                } 
                animator.SetBool ("isGrounded", grounded);
            }
        }
    } 

PlayerShoot method

IEnumerator PlayerShoot()
    {
        while(true)
        {
            if (!transform.GetChild
                (0).gameObject.GetComponent<PlayerHealth>().hasDied)
            {
                if (energy >= 5f)
                {
                    //energy -= 5f;
                    energyLevel.value = (int)
                        energy;
                    energyText.text = (int)energy 
                        + "%";

                    GameObject newProjectile = 
                        Instantiate(projectile, new Vector3(transform.position.x + 1.5f, transform.position.y - 0.02f, 
                            transform.position.z), Quaternion.identity) as GameObject;
                    if 
                        (GameController.instance.isMusicOn)
                    {

                        AudioSource.PlayClipAtPoint(MusicController.instance.audioClips[2], 
                            newProjectile.transform.position);
                    }
                }
            }

            yield return new WaitForSeconds(shootDelay); 
            //Wait for 'shootDelay' seconds before starting over again.
        }
    }
\$\endgroup\$
  • \$\begingroup\$ I think you should stop the shoot coroutine when you detect Input.GetButtonUp("Fire1") \$\endgroup\$ – Hellium Sep 18 '17 at 9:02
0
\$\begingroup\$

Try something like this. I got rid of the coroutine, tell me if it's ok for you

private float lastShootTime = 0 ;
private PlayerHealth health ;

public Graphic button; // Drag and drop a UI image / Raw Image
private bool buttonDown = false;

// Indicates whether the player can shoot
private bool canShoot
{
    get { !health.hasDied && energy >= 5f && Time.time >= lastShootTime + shootDelay ; }
}

void Start()
{
    // Retrieve **once** the PlayerHealth script
    health = transform.GetChild(0).gameObject.GetComponent<PlayerHealth>();

    // Add event listeners to the button to detect whether the user is holding the button
    button.raycastTarget = true;
    EventTrigger eventTrigger = button.GetComponent<EventTrigger>();
    if (eventTrigger == null)
        eventTrigger = button.gameObject.AddComponent<EventTrigger>();

    EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
    pointerDownEntry.eventID = EventTriggerType.PointerDown;
    pointerDownEntry.callback.AddListener( (data) => { buttonDown = true; } );
    eventTrigger.triggers.Add( pointerDownEntry );

    EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
    pointerUpEntry.eventID = EventTriggerType.PointerUp;
    pointerUpEntry.callback.AddListener( (data) => { buttonDown = false; } );
    eventTrigger.triggers.Add( pointerUpEntry );

    EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
    pointerExitEntry.eventID = EventTriggerType.PointerExit;
    pointerExitEntry.callback.AddListener( (data) => { buttonDown = false; } );
    eventTrigger.triggers.Add( pointerExitEntry );
}

void Update()
{
    if( buttonDown && canShoot )
    {
        // No coroutine anymore
        // The "wait" between 2 bullets is handled inside the `canShoot` property
        Shoot();
    }
}

// Shoot the bullet!
private void Shoot()
{
    energyLevel.value = (int) energy;
    energyText.text = (int)energy + "%";

    Vector3 projectilePosition = new Vector3(transform.position.x + 1.5f, transform.position.y - 0.02f, transform.position.z)
    GameObject newProjectile = Instantiate(projectile, projectilePosition, Quaternion.identity) as GameObject;
    if (GameController.instance.isMusicOn)
    {
        AudioSource.PlayClipAtPoint(MusicController.instance.audioClips[2], projectilePosition);
    }
    lastShootTime = Time.time;
}
\$\endgroup\$
  • \$\begingroup\$ its work thanks :) but when i jump its fire. \$\endgroup\$ – user107296 Sep 19 '17 at 6:15
  • \$\begingroup\$ If you want to prevent your player from shooting when jumping, add grounded at the end of the condition inside the canShoot : get { !health.hasDied && energy >= 5f && Time.time >= lastShootTime + shootDelay && grounded; } Also, make sure your coroutine PlayerShoot is removed from your code and you don't call Shoot outside the Update function \$\endgroup\$ – Hellium Sep 19 '17 at 6:24
  • \$\begingroup\$ actually my player fire with specific gap but i touch anywhere on the screen its fire. \$\endgroup\$ – user107296 Sep 19 '17 at 6:49
  • \$\begingroup\$ I suppose your game is a mobile one, and I guess you want to have specific on-screen buttons to fire? Then, I invite you to read this thread to detect touch events on ui gameobjects. \$\endgroup\$ – Hellium Sep 19 '17 at 6:58
  • \$\begingroup\$ Yes it is for mobile purpose. so, what to do for my buttons? \$\endgroup\$ – user107296 Sep 19 '17 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy