Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Posthuman-Wizard · Aug 12, 2017 at 07:07 PM · physicscoroutinesienumeratorinstantiationspeed up

Generating Objects at High Speeds

Hey everyone!

I'm having an issue with generating objects at variable speeds in my game. Currently, I am generating a vertical "line" of game objects every so often at a set speed, which works fine, as long as the speed isn't too high. However, if I adjust the speed so that it is higher, and/or temporarily increase the speed and slow it back down via a coroutine, the objects will start generating inconsistently and spaced out instead of at the same consistent rate. To illustrate this problem, here is how the game looks (scene view top, game view bottom) before the coroutine is started:

alt text

And here is how it looks like after the coroutine is started and ended:

alt text

(Weirdly, the first column of the generated objects appears warped, so I'm not sure what's going on there. Could be an unrelated issue.)

I first thought this was due to not taking into account the speed modifier during the coroutine when generation happens, so I tried applying that the values that seemed relevant, but that didn't work (also, the problem only seems to happen after the coroutine completes, making me believe that this might not be the issue).

I'm reusing instantiated objects: the game will instantiate new objects if it can't find any within the pool, and if it can, it will simply reactivate them and use them again.

Here's the object generation code as well as the speed up coroutine.

 private void OnDespawn(PoolableObject poolable)
     {
 
         if (poolable is NPCLine)
         {
 //This is throwing an invalidoperationexception when it goes too fast
           //  NPCLine despawnedLine = poolable as NPCLine;
         //    var item = npcLineMatrix.First(kvp => kvp.Value == despawnedLine);
          //   npcLineMatrix.Remove(item.Key);
         }
     }
   
    //This coroutine is pretty clunky as-is; there's probably a better way to achieve the effect

     IEnumerator BasicAttack()
     {
         float timer = 0f;
         float t = 0f;
      
         float minimum = 1f;
         float maximum = 4f;
         float partOne = 1f;
         float partTwo = 2f;
         float partThree = 3f;
         invulnerable = true;
         moveSpeedAttackMultiplier = 1f;
         while (timer < partOne)
         {
             moveSpeedAttackMultiplier = Mathf.Lerp(minimum, maximum, Mathf.SmoothStep(0.0f, 1.0f, t));
             timer += Time.deltaTime;
             t += Time.deltaTime;
             yield return null;
         }
         moveSpeedAttackMultiplier = maximum;
         while (timer < partTwo)
         {
             timer += Time.deltaTime;
             yield return null;
         }
         float temp = maximum;
         maximum = minimum;
         minimum = temp;
         t = 0f;
         while (timer < partThree)
         {
             moveSpeedAttackMultiplier = Mathf.Lerp(minimum, maximum, Mathf.SmoothStep(0.0f, 1.0f, t));
             timer += Time.deltaTime;
             t += Time.deltaTime;
             yield return null;
         }
         invulnerable = false;
         moveSpeedAttackMultiplier = 1f;
         yield return null;
     }
 
 void OnFixedTick()
     {
         float timestep = moveSpeed * cameraSize * moveSpeedAttackMultiplier;
         this.mRigidBody.velocity = new Vector2(timestep, 0);
         if (prepareTimer < ttPrepare)
             prepareTimer += Time.deltaTime;// * timestep;
         int maxX = npcLineMatrix.Keys.Max();
         //If the new position to spawn at is within the horizontal bounds
         float spawnX = npcLineMatrix[maxX].transform.position.x + spawnDistanceX;
         float specialSpawnY = maxSpecialSpawnY * cameraSize;// * moveSpeedAttackMultiplier;
         if (spawnX < this.transform.position.x + (this.baseNPCSpawnMaxX * cameraSize * moveSpeedAttackMultiplier))
             SpawnLine(spawnX, false, specialSpawnY);
     }
 
     float lastTime;
 
     void SpawnLine(float spawnX, bool neutralOnly, float specialSpawnYMax)
     {
         if (lastTime != null) Debug.LogError(Time.time - lastTime);
         lastTime = Time.time;
         float maxY = this.baseLineHeight * cameraSize;// * moveSpeedAttackMultiplier;
         if (even) npcLineMatrix.Add(npcLineCount, SpawnEvenLines(spawnX, (int)maxY, neutralOnly, specialSpawnYMax));
         else npcLineMatrix.Add(npcLineCount, SpawnOddLines(spawnX, (int)maxY, neutralOnly, specialSpawnYMax));
         npcLineCount++;
         even = !even;
     }
 
 NPCLine SpawnEvenLines(float newX, int newMaxY, bool neutralOnly, float specialSpawnYMax)
     {
        // Debug.LogError("spawning even line at " + newX);
         NPCLine newLine = Pool.SpawnNewNPCLine(new Vector3(newX, 0, 0));
         newLine.transform.position = new Vector3(newX, 0, 0);
         for (int i = -newMaxY + 1; i < newMaxY; i += 2)
         {
             if (neutralOnly || prepareTimer < ttPrepare || Mathf.Abs(i * spawnDistanceY) > specialSpawnYMax) newLine.SpawnNPC(NPC.NPCState.NEUTRAL, new Vector3(newX, i * spawnDistanceY, i));
             else if (!boss && Random.Range(0, 100) < bonusChance) newLine.SpawnNPC(NPC.NPCState.BONUS, new Vector3(newX, i * spawnDistanceY, i));
             else if (!boss && Random.Range(0, 100) < grumpChance) newLine.SpawnNPC(NPC.NPCState.GRUMP, new Vector3(newX, i * spawnDistanceY, i));
             else newLine.SpawnNPC(NPC.NPCState.NEUTRAL, new Vector3(newX, i * spawnDistanceY, i));
         }
         return newLine;
     }
 
     NPCLine SpawnOddLines(float newX, int newMaxY, bool neutralOnly, float specialSpawnYMax)
     {
         //Debug.LogError("spawning odd line at " + newX);
         NPCLine newLine = Pool.SpawnNewNPCLine(new Vector3(newX, 0, 0));
         newLine.transform.position = new Vector3(newX, 0, 0);
         for (int i = -newMaxY; i < newMaxY + 1; i += 2)
         {
             if (neutralOnly || prepareTimer < ttPrepare || Mathf.Abs(i * spawnDistanceY) > specialSpawnYMax) newLine.SpawnNPC(NPC.NPCState.NEUTRAL, new Vector3(newX, i * spawnDistanceY, i));
             else if (!boss && Random.Range(0, 100) < bonusChance) newLine.SpawnNPC(NPC.NPCState.BONUS, new Vector3(newX, i * spawnDistanceY, i));
             else if (!boss && Random.Range(0, 100) < grumpChance) newLine.SpawnNPC(NPC.NPCState.GRUMP, new Vector3(newX, i * spawnDistanceY, i));
             else newLine.SpawnNPC(NPC.NPCState.NEUTRAL, new Vector3(newX, i * spawnDistanceY, i));
         }
         return newLine;
     }

Any advice or help would be greatly appreciated (and I'll happily provide more info if it helps). Thanks in advance! :)

problem-screencap-0.png (463.9 kB)
problem-screencap-1.png (321.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine not working properly 1 Answer

Coroutine couldn't be started because the the game object 'Attack' is inactive! 1 Answer

Run Coroutine once, but finish Lerping 2 Answers

Coroutines not passing yield 1 Answer

Having trouble using coroutines, making unity hang 0 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