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 /
  • Help Room /
avatar image
0
Question by UDN_b41d082b-b87f-457a-a112-fc5a0b2f68ad · Mar 25, 2017 at 03:10 PM · coroutinesupdate functiongarbage-collection

use Update instead of Coroutines

I have a Prefab that gets instantiated everytime a ray (casted by a touch input) hit a gameobject that's in the layer mask of the ray, that prefab has to move from the position (Screen.width / 2, 0) to (Screen.width / 2, Screen.Height / 2). Originally I made it move by using a coroutine started at Start method, but after I read this https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity-games?playlist=44069 I got that it would be better not to use too many coroutines to avoid GC issue. And that is my case because I start a new coroutine for every prefab instantiated. This is the code I used inside the coroutine to make prefab move:

 var currentPos = transform.localPosition;
 var t = 0f;
 while (t < 1) {
     t += Time.deltaTime / timeToMove;
     transform.position = Vector3.Lerp (currentPos, endPos, t);
     yield return null;
 }

The question is: How can I use that code in Update function?

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

1 Reply

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

Answer by Pengocat · Mar 25, 2017 at 06:38 PM

Something along these lines should do the trick.

     public float timeToMove = 2f;
 
     private Vector3 startLerpPos;
     private Vector3 endLerpPos;
     private float currentLerpTime = -1f;
     private float t;
 
     void Update()
     {
         if (Input.touchCount > 0)
         {
             BeginLerp(Vector3.one);
         }
         Lerping();
     }
 
     public void BeginLerp(Vector3 destination)
     {
         startLerpPos = transform.localPosition;
         endLerpPos = destination;
         currentLerpTime = 0f;
     }
 
     private void Lerping()
     {
         if (currentLerpTime > timeToMove || currentLerpTime < 0f)
         {
             return;
         }
         else
         {
             currentLerpTime += Time.deltaTime;
             t = currentLerpTime / timeToMove;
             transform.position = Vector3.Lerp(startLerpPos, endLerpPos, t);
         }
     }

Just keep in mind that you should avoid "premature optimization". Chances are, what you think is slowing things down is actually insignificant compared to other things in your code. Use the profiler to find the worst parts and spend time fixing those first.

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 UDN_b41d082b-b87f-457a-a112-fc5a0b2f68ad · Mar 26, 2017 at 10:14 AM 0
Share

The game is actually not slow, I just wanted to be sure I was doing everything fine and optimized, so thanks for your advice about "premature optimization" and for your code, it worked well.

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

97 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

Related Questions

Using Coroutines and Update together 0 Answers

Unity C# Punching Coroutine not completing 0 Answers

Which is more efficient, Update() or a couroutine with while(true)? 1 Answer

garbage collection in between scene changes? 2 Answers

Trying to use coroutines in order to use Mathf.Lerp, Lerp instantly jumps values 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