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
0
Question by Dreoh · Apr 11, 2012 at 08:43 PM · coroutineloopyieldmelee

While loop yield lag?

I'm trying to make a script that when I use a melee attack, the player moves towards the enemy unless he is within a specific distance to him, and the enemy is also pushed back, regardless of position.

I got it to work.. the only problem is that it seems that because I have a nested while loop... It seems to have the first frame lag for a noticeable split-second before working as it should.

This lag is a problem, since I can't have the game lag every time the player wants to melee. Is there any reason besides a slow cpu that this would happen, I really doubt my computer wouldn't be able to handle this, and is there any way I can make it more efficient?

 var cooldownTimer: float = 10.0; //This will be changed in inspector
 var countDown : float = 0.0;
 var playerAttackRange : int = 5;
 var particle : GameObject;
 var player : GameObject;
 var particleClone = particle;
 var slashDistance : float = 1.0;
 var moveDirection;
 var shouldMove : boolean = true;
 var controller : CharacterController;
 private var currentPosition : Vector3;
 var swordSlash : AudioClip;
 var hit : RaycastHit;
 var hitLoc;
 var impact;
 var attackBool : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
     
     currentPosition = transform.position;
     
     moveDirection = player.transform.forward;
     
     if(Input.GetButton("Fire1") &&  Time.time > countDown){
                 
         attack();
         
     }
         
 }
 
 
 function attack(){
   if(AbilitySystem.fireElement.equippedFire == true) {
     audio.PlayOneShot(swordSlash);
     countDown = Time.time + cooldownTimer;
     var fwd = transform.forward; // <- easier way to get the forward direction
     if(Physics.Raycast(transform.position, fwd, hit, playerAttackRange)){
       Debug.Log("Melee has struck " + hit.collider.name + " at a range of " + playerAttackRange +"!!!!!!!!!!!");
       if(hit.collider.tag == "Enemy") {
         hit.collider.GetComponent(EnemyHealth).enemyCurrentHealth -= AbilitySystem.fireElement.swordDamage;
         particleClone = Instantiate(particle, hit.transform.position, hit.transform.rotation);
         Destroy(particleClone, 1);
         hitLoc = hit.point;
         // calculate the direction to push the target:
         impact = -hit.normal;
         impact.y = 0; // keep it horizontal
         impact.z = 0; // keep it 2d
         impact = impact.normalized;
         // apply the movement during duration seconds:
         var duration = 0.25;
         var durationTwo = duration;    
         while (duration > 0){
             Debug.Log("While loop one started");
             while (durationTwo > 0){
                 Debug.Log("While loop two started");
                 if(Vector3.Distance(transform.position, hitLoc) > slashDistance){    
                     player.transform.Translate(impact.x * Time.deltaTime * 4, impact.y, impact.z, Space.World);
                     Debug.Log("Player transformed");
                 }
                 durationTwo -= Time.deltaTime;
                 Debug.Log("DurationTwo = " + durationTwo);
             }     
             hit.transform.Translate(impact * Time.deltaTime * 4, Space.World);  
             Debug.Log("Enemy transformed");         
               duration -= Time.deltaTime; // count time
               Debug.Log("Duration = " + duration);
               Debug.Log("yielding");
               yield; // return to loop next frame
         }
       }
     }
   }
 }
Comment
Add comment · Show 1
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 Dreoh · Apr 11, 2012 at 09:36 PM 0
Share

It actually seems to lag more the farther away the player is from the enemy

1 Reply

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

Answer by Kryptos · Apr 11, 2012 at 10:12 PM

Your nested while does not wait with a yield statement. Try with:

 while (duration > 0){
     Debug.Log("While loop one started");
     while (durationTwo > 0){
         Debug.Log("While loop two started");
         if(...){
             ...
         }
         ...
 
         Debug.Log("yielding");
         yield; // return to loop next frame
     }
     ...
 
     Debug.Log("yielding");
     yield; // return to loop next frame
 }

But it seems that you want the two while to be executed in parallel. You should consider using two coroutines (one for each while).

Comment
Add comment · Show 1 · 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 Dreoh · Apr 11, 2012 at 10:19 PM 0
Share

How would I easily pass the required variables to the coroutines? The reason I did it this way was because I needed the variables gained from the RayCastHit

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause the coroutine inside a loop 1 Answer

Coroutine For Loop Will Not End 1 Answer

Call long running AI function without tying up game loop? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Waiting twice inside coroutine (C#) 2 Answers


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