1

enter image description here

my problem is that all of three enemys shoot at the same time. i want first then second and then the third one start shooting.

here is my code :

public float speed = 7f;
public float attackDelay = 3f;
public Projectile projectile;

private Animator animator;

public AudioClip attackSound;

void Start () {
    animator = GetComponent<Animator> ();

    if(attackDelay > 0){

        StartCoroutine(onAttack());
    }
}

void Update () {
    animator.SetInteger("AnimState", 0);
}

IEnumerator onAttack(){
    yield return new WaitForSeconds(attackDelay);
    fire();
    StartCoroutine(onAttack());
}

void fire(){
    animator.SetInteger("AnimState", 1);

    if(attackSound){
        AudioSource.PlayClipAtPoint(attackSound, transform.position);
    }
}
void onShoot(){
    if (projectile){
        Projectile clone = Instantiate(projectile, transform.position, Quaternion.identity)as Projectile;
        if(transform.localEulerAngles.z == 0){
            clone.rigidbody2D.velocity = new Vector2(0, transform.localScale.y) * speed * -1;
        }
        else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 90){

            clone.rigidbody2D.velocity = new Vector2 (transform.localScale.x, 0) * speed;
        }
        else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 180){
            clone.rigidbody2D.velocity = new Vector2 (0, transform.localScale.y) * speed;
        }
        else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 270){
            clone.rigidbody2D.velocity = new Vector2(transform.localScale.x, 0) * speed * -1;
        }
}

}

onShoot() method is called as an event in the animation.

do you guys have any suggestions for this?

1
  • the "InvokeRepeating()" method also can be used for this situation. Apr 3, 2015 at 21:01

2 Answers 2

3

Well, one way (albeit probably not the best) is to add a delay within Start(). You can have something like:

public float startDelay;
...
void Start()
{
    ...
    StartCoroutine(startDelay());
}

IEnumerator startDelay()
{
    yield return new WaitForSeconds(startDelay);
    StartCoroutine(onAttack());
}

With that you just set startDelay as you see appropriate. Because it's a public variable, you can set it in the inspector for the respective gameObject (if you set it in the script, every object will have may have the same delay, making no difference).

Another approach may be to randomize the attackDelay. Use Random.Range to determine the attackDelay whilst keeping it within a reasonable range.

I think you may also want to reconsider how your enemies function. Maybe have them shoot when the player crosses a trigger or put in some logic that has them shoot when the player gets within range.

0
0

I'd just save a static boolean property somewhere. Let's name it "Can Shoot". I'd make it false when an enemy shoots and make it true after a shoot delay.

public float shooting delay;    

bool _canShoot;
public static bool canShoot
{
    get { return _canShoot; }
    set { _canShoot = value; StartCoroutine(EnableShooting()); }
}

IEnumerator EnableShooting()
{
    yield return new WaitForSeconds(delay);
    _canShoot = true;
}

Now just modify your shooting method to only shoot if the "canShoot" property is true

void onShoot(){
if (projectile && canShoot){ 
    canShoot = false;
    ... 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.