Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by SrBilyon · Nov 03, 2010 at 08:01 AM · aienemylagwaitforsecondswait

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by StephanK · Nov 03, 2010 at 08:30 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Nov 03, 2010 at 06:06 PM 0
Share

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

avatar image StephanK · Nov 03, 2010 at 10:15 PM 0
Share

$$anonymous$$ake sure you're calling Attack() only once to start attacking. $$anonymous$$aybe you can post your current logic?

avatar image
0

Answer by zannghast · Nov 03, 2010 at 08:46 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why is this script causing a lot of lag? 2 Answers

Problem getting AI to wait 1 Answer

How to make an object jump? 0 Answers

switch prefab on collision 1 Answer

Need help with my enemy spawn script. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges