• 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 Bhanusrimattey · Sep 18, 2020 at 05:39 AM · movetowardsupdate function

How to detect changing values in update

Here is my code of passing gameobject to animate them. The animate code first scales gameobject and then returns back to original position

Here is the code where i pass gameobject to animate them.

 for (int j = 0; j < gridSize; j++)
         {
             time = 0;
             AnimatePlayObject(TileParent.transform.GetChild(fixedStart).transform.gameObject.GetComponent<Tile>().ObjectLocked);

             fixedStart+=gridSize;
         }


Here is the code for animating gameobject

  private void AnimatePlayObject(GameObject go)
 {
     Vector2 finalScale = new Vector2(0.8f,0.8f);
     Vector2 initialScale = new Vector2(0.5f, 0.5f);
    
     if (time == 0)
     {
         go.transform.localScale = Vector2.MoveTowards(go.transform.localScale, finalScale, 0.005f);
         
         if (go.transform.localScale.Equals(finalScale))
         {
             time = -1;
         }
     }

     if (time == -1)
     {
         go.transform.localScale = Vector2.MoveTowards(go.transform.localScale, initialScale, 0.005f);
         if (Mathf.Abs(go.transform.localScale.x - initialScale.x) < 0.02f && Mathf.Abs(go.transform.localScale.y - initialScale.y) < 0.02f)
         {
             time = 2;
         }
     }

}

I think the problem is both functions are in update function time is always set to zero .After the gameobjects reach finalScale it is not returning back to initialScale as written by code because time = -1 after it reaches finalScale.

Please someone help me to have time value updated to -1 after it reaches finalScale .Here time is always zero

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

Answer by Laiken · Sep 18, 2020 at 06:07 AM

Just to be sure, change line 10 to:

 if (go.transform.localScale.x >= finalScale.x)

Also, for the cases like line 8, we normally do like this:

 go.transform.localScale = Vector2.MoveTowards(localScale, finalScale, someSpeed * Time.deltaTime);

The reason is so that the fps won't affect the speed of the animation/movement

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 Bhanusrimattey · Sep 18, 2020 at 06:45 AM 0
Share

I want it to equal why is should i change to >= and thanks for the suggestion i will multiply with Time.deltaTime

avatar image Laiken Bhanusrimattey · Sep 18, 2020 at 10:49 PM 0
Share

Sorry I wasn't clear. I asked you to put '>=' just to be sure if the problem is that the 'go.transform.localScale' is actually not turning exactly equal to 'finalScale'.

But I read your code again. You said you were putting 'for (int j = 0; j < gridSize; j++)' inside the 'Update()' right? It has 'time = 0' inside it. Every frame it changes the time back to 0. What you need to do is remove the line 'time = 0' from there and put it outside the update.

You may want to consider using coroutines to handle cases like AnimatePlayObject btw

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

209 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Shoot bullet towards mouse 0 Answers

Vector3.MoveTowards moving transform 0 Answers

Vector2.MoveTowards not working. 0 Answers

Having trouble increasing level inside of Update() when score reaches 1000... 0 Answers

We just updated to 5.6.0f3 and none of our machines cannot load the unityengine.ui 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