• 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
Question by darkavengerpark · Apr 14, 2022 at 08:22 AM · lerpconditional

How can I optimize this Lerp movement in the Update Function?

I scripted a lerp animation in the Update function because my Coroutine code didn't work properly. So, in order to optimize it a bit, I tried using some guard clauses, but I don't even know if the logic is right. Is there a way to optimize it?

public GameObject[] GameMode; //end position

     public GameObject startPositionGameMode;     //start positions
     public GameObject[] finalPositionGameMode;
     
     public float speed = 0.5f;
     float current, target;

     [SerializeField] private AnimationCurve curve;

     public float radius=1;
     public Transform cube;

     public bool animationComplete =true;
 
     private void Update() {
         CheckState();
     }
 
     private void CheckState() {
         Vector3 distance = transform.position - cube.transform.position;
 
         if (distance.magnitude <= radius) {
             target = 1;
         }
         else {
             target = 0;
         }
 
         if (!animationComplete) return;
 
         animationComplete = false;
         Animate();
 
         Debug.LogWarning("CheckState" + animationComplete);
     }
 
     private void Animate() {
         Debug.LogWarning("Animate" + animationComplete);
 
         current = Mathf.MoveTowards(current, target, speed * Time.deltaTime);
 
         for (int i = 0; i < GameMode.Length; i++) {
            GameMode[i].transform.position = Vector3.Lerp(startPositionGameMode.transform.position, finalPositionGameMode[i].transform.position, curve.Evaluate(current));
         }
         animationComplete = true;
     }
Comment

People who like this

0 Show 4
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 Pangamini · Apr 14, 2022 at 01:02 PM 0
Share

I have no idea what are you trying to optimize. Is it working as inteded, but too slow? Anyway, you could change if (distance.magnitude <= radius) to if (distance.sqrMagnitude <= radiusSquared) {To avoid the unnecessary sqrt..

avatar image darkavengerpark Pangamini · Apr 14, 2022 at 04:19 PM 0
Share

Well, most of the code is run in the Update function so I thought it would be very costly. Also, for some reason, the animationComplete variable seems to only work when it's true, and doesn't change... Maybe this will also affect the guard clauses I put up?

avatar image darkavengerpark darkavengerpark · Apr 14, 2022 at 04:22 PM 0
Share

Sorry, my question was written very unclear. My initial question was how can I optimize the code when utilizing the Update function. But I'm more interested in whether the code logic is sturdy.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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 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

Not Sure How to Get Around this Problem 1 Answer

changes to colour alpha affect RGB values, but not in the same way 1 Answer

Smoothing end of a lerp 2 Answers

Lerp color of verts with position bigger than 1 1 Answer

Lerping doesn't return to the same position? 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