3

My player Robot is attacking when I click on the attack button as follows. I am using a coroutine to wait for some time then instantiate laser form firepos position.

Please see the below gif for clear understanding: error in shoot time

Now if u see the above gif you can see that if I press and hold the attack button the player is doing his animation and according to waitForSeconds coroutine after one sec the laser shoots. Its fine till now !

But when I suddenly tap on button many times within a small time the animation doesn't plays(which I can understand..its obvious!) but just like the above case after some time(due to waitforseconds) and due to no of times I tapped the same no of lasers shoot which I DO NOT WANT !

what I want is that when I click and hold the attack button the animation should complete and then the laser should shoot .

AND

If I tap it many times within a small time the laser(which is a prefab getting instantiated ) should not shoot.

The laser should shoot only when animation gets completed.

Now I am not able to figure that out ! Can any body help.

for button (attack ) in inspector :

laser button

I am calling two functions in OnpointerDown and up

functions code:

public void LaserAttack(){
        isAttacking = true;
        StartCoroutine (AttackStillCoroutine ());

        if (robotLeft == true&&isLaserLeft==false) {

            //Debug.Log (firep.position.x);

            //Instantiate (leftLaser, firep.position*1.29f, Quaternion.identity);

            StartCoroutine( DelayMethodLeftInstantiate());

        } 

        if (robotRight == true&&isLaserLeft==false) {

            StartCoroutine(  DelayMethodRightInstantiate());

            //Instantiate (RightLaser, firep.position, Quaternion.identity);
        }
        anim.SetBool ("isAttack", isAttacking);
        isLaserLeft = true;

    }

    public void LaserAttackEnd(){
        isAttacking = false;
        attackStill = false;
        anim.SetBool ("isAttackStill",attackStill);
        anim.SetBool ("isAttack", isAttacking);
        isLaserLeft = false;

    }

and Coroutine code(there are two for left and right):

IEnumerator DelayMethodLeftInstantiate(){

        yield return new WaitForSeconds(1);
        Instantiate (leftLaser, firep.position, Quaternion.identity);
    }

    IEnumerator DelayMethodRightInstantiate(){

        yield return new WaitForSeconds(1);
        Instantiate (RightLaser, firep.position, Quaternion.identity);
    }

Please help and sorry for lack of knowledge ...Guys Im noob !

utkdub
  • 274
  • 2
  • 12
  • There is such answers, I guess google is the best answer in such cases : [answer](http://answers.unity3d.com/questions/37411/how-can-i-wait-for-an-animation-to-complete.html) – Shakra Jan 23 '17 at 06:05

1 Answers1

1

There could be two ways to fix your issue.

First you could stop the coroutine if the button is release.

You could use the IPointerUpHandler (I assume you call the method from UI button)

public class InputHandler, MonoBehaviour, IPointerUpHandler
{
    IEnumerator coroutine = null;
    public void LaserAttack(){
        isAttacking = true;
        this.coroutine = AttackStillCoroutine ()
        StartCoroutine (this.coroutine);
       // More code
    }
    public void OnPointerUp(PointerEventData eventData)
    {
         if(this.isAttacking == true && this.coroutine != null ){
             StopCoroutine(this.coroutine);
             this.coroutine = null;
         }
    }
}

Another way that I recommend is to use AnimationEvent. You can place a method delegate to your animation so that when it reaches it, the method gets called.

I'll paste a link as it is more visual than code:

https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Everts
  • 10,408
  • 2
  • 34
  • 45
  • That `IpointerUpHandler` and `DownHandler` I am already using for continuous movement (right-left) when click on UI `event Trigger` `PointerDown` and up handlers .I don't want to add much more code to make it more complex. But your *2nd* `AnimationEvent` way is best ,awesome ! – utkdub Jan 23 '17 at 08:48