x


Add wait time to enemy's attack

Here is the code I made for the enemy's attack. This function is called by another script (in C#) using SendMessage("Attack");

function Attack()
    {
    yield WaitForSeconds(attackLag); 
     var myself : EnemyDamage = GetComponent(EnemyDamage);

     if (!myself.flinch)
     {
    // Keep looking if we are hitting our target
    // If we are, knock them out of the way, dealing damage
    Debug.Log("I'm Attacking");
    animation.Play("punch");

        var pos = transform.TransformPoint(punchPosition);
        if(Time.time > lastPunchTime + 4 && (pos - target.position).magnitude < punchRadius)
            {
                // deal damage
                target.SendMessage("ApplyDamage", damage);
                // knock the player back and to the side
                var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
                slamDirection.y = 0;
                slamDirection.z = strength;
                if (slamDirection.x >= 0)
                    slamDirection.x = 0.1;
                else
                    slamDirection.x = -0.1;
                target.SendMessage("Slam", transform.TransformDirection(slamDirection));
                lastPunchTime = Time.time;

            }
        }

    }

I tried using yield WaitForSeconds, but it only lags the first attack, then the next few are consecutive. What should I do?

more ▼

asked Nov 03 '10 at 08:01 AM

SirVictory gravatar image

SirVictory
1.7k 64 77 104

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I guess the problem is that you are calling Attack in rapid succession. So the effect you'd see is that the first call gets delayed and all others get called immediately after the first one.

What you need to do is using Attack as a Coroutine with a while loop inside. So you could transform your code into something like this (this should only give you an idea, it will not work as is):

function Attack() {
  while ((pos - target.position).magnitude < punchRadius) {
    target.SendMessage("ApplyDamage", damage);
    yield WaitForSeconds(attackLag);
  }
}

Be aware that you have to call this routine only ONCE to start attacking. It will then continue to attack the target until it's out of range. So probably you'll also need a check if target died.

more ▼

answered Nov 03 '10 at 08:30 AM

StephanK gravatar image

StephanK
6k 39 53 93

I tried using this method with my current logic, but I end up hitting hit like 4 times after the lag.

Nov 03 '10 at 06:06 PM SirVictory

Make sure you're calling Attack() only once to start attacking. Maybe you can post your current logic?

Nov 03 '10 at 10:15 PM StephanK
(comments are locked)
10|3000 characters needed characters left

An alternate solution would be to 'deactivate' function 'Attack()' by setting a state in the beginning and end.

Such as:

// global variable
var attacking : bool = false;

function Attack() {

    if (!attacking) {
        attacking = true;
        ...
        //rest of the code
        ...
        attacking = false;
    }
}

If I'm not mistaken, attacking would only be set to false only after attacklag seconds has passed.

more ▼

answered Nov 03 '10 at 08:46 AM

zannghast gravatar image

zannghast
526 11 14 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x959
x653
x196
x170
x58

asked: Nov 03 '10 at 08:01 AM

Seen: 2352 times

Last Updated: Nov 03 '10 at 08:01 AM