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
1
Question by hirenkacha · May 14, 2012 at 05:10 AM · texture2donguitime.deltatime

move texture 2d with Time.deltatime

I want to move a texture 2d on touch end. currently I have functionality of setting the texture 2d to the center on Touch end. i want it to be look smoothly come to the center. I am using this script to do so. my texture2d's y position is stored in padYpos. this is in OnGUI( )

 if((Input.GetTouch(0).phase==TouchPhase.Ended) || (Input.GetTouch(0).phase==TouchPhase.Canceled))
             {
                 if(dist>0f)
                 {
                     while (padYpos>center.y)
                     {
                         padYpos+=Time.deltaTime*5f;
                         dist=center.y-padYpos;
                     }
                 }
                 if(dist<0f)
                 {
                     while (padYpos<center.y)
                     {
                         padYpos-=Time.deltaTime*10f;
                         dist=center.y-padYpos;
                     }
                 }
             }
 GUI.DrawTexture(new Rect(padXpos, padYpos, padWidth, padHeight), pad);
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

3 Replies

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

Answer by hirenkacha · Jun 11, 2012 at 05:25 AM

I found the solution. I am not using while loop now as told by Walframe. So I am using the code in simple update ().

 if(dist>0f)
       {
              padYpos+=Time.deltaTime*5f;              
       }
 else
 {
              padYpos-=Time.deltaTime*5f;
 }
Comment
Add comment · 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
1

Answer by Berenger · May 14, 2012 at 05:48 AM

You shouldn't use Input in OnGUI. Put it in Update instead. Then, you can start a coroutine on touch that will interpolate dist minVal to maxVal in x seconds. The GUI.DrawTexture, however, must stay in OnGUI.

Comment
Add comment · Show 3 · 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 hirenkacha · May 14, 2012 at 07:58 AM 0
Share

ok... so i shld use all calculations in Update(). But is my logic correct..???

avatar image hirenkacha · May 14, 2012 at 10:41 AM 0
Share

I have put Input and all the calculation in Update function but its still not working any idea y???

avatar image Berenger · May 14, 2012 at 12:56 PM 0
Share

I am not familiar with touch methods, but I guess Ended and Canceled are ponctual. Then, your code will only be executed once. That's why the movement shouldn't happen here but inside a coroutine started there.

avatar image
1

Answer by Wolfram · May 14, 2012 at 02:22 PM

Apart from what Bérenger said, you cannot use/see changes to your scene done in a simple while loop, since it will never render the scene while inside the loop. Update() (and OnGUI()) are called exactly ONCE per frame, so your scene will appear to be stuck while the loop is fully executed, and there will be no updates inbetween.

As Bérenger suggests, use Update(), launch a Coroutine instead of putting the while-loop directly in Update(). But then move each while-loop to a Coroutine, with "yield return 1;" (in C#) being the last statement within the loop.

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 hirenkacha · May 16, 2012 at 09:11 AM 0
Share

hmm.. let me try this too...

avatar image hirenkacha · May 18, 2012 at 04:50 AM 0
Share

I am not able to do this.. can u help me by sample??

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

6 People are following this question.

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

Related Questions

Making texture cover whole button 1 Answer

When to use OnGUI() ? 1 Answer

My OnGUI() Won't show the Button elements :( 0 Answers

Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers

"null texture passed" for any texture2D other than the 2 originals. 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