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
0
Question by oliver-jones · May 13, 2011 at 02:51 PM · animationguiupdateframerate

Animating GUI Texture - Help

Hello,

I'm trying to animate this GUI texture that basically is off screen, and then slides in to the left.

At first, I was using an update function to do the animation like this: (snippet)

private var panelAnimation : float = -800; // start = -800 || finish = 0 var slideOpenPanel : boolean = true;

function Update(){

 if(panelAnimation < 0 && slideOpenPanel == true){
     panelAnimation += 1 * 30;
 }
 if(panelAnimation > -800 && slideOpenPanel == false){
     panelAnimation -= 1 * 50;
 }

}

function OnGUI(){ GUI.BeginGroup (new Rect (panelAnimation, Screen.height-200, 800, 200)); }

But I soon found out that the animation wouldn't stop exactly at '0', sometimes it would stop a few pixels out - this is because the update (frame rate) cant keep up with the calculation.

How would I do this so the animation would stop exactly at '0'?

Thanks

---- UPDATE ----

Here is another GUI window that slides in, but from the right:

private var panelInfoAnimation : float = 0; // start = 0 || finish = -200 var slideOpenPanelInfo : boolean = true;

if(panelInfoAnimation > -200 && slideOpenPanelInfo == true){ panelInfoAnimation += -1 * infoPanelSpeed_in; }

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

4 Replies

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

Answer by Joshua · May 13, 2011 at 03:24 PM

Right now you're checking if it's current position is within bounds and then move it by x. The result is that the current position + x might be out of bounds, resulting in the inpresise result you're mentioning.

Before actually moving it you should also check if the end-result won't overshoot:

if(panelAnimation < 0 && slideOpenPanel == true){

 if(panelAnimation + 30*Time.deltaTime &lt; 0 ) { //will the moving not make us overshoot?
     panelAnimation += 30 * Time.deltaTime;
 }
 else { //else we'll move by less so we end up at 0
     panelAniumation = 0;
 }

}

cjmarsh his answer will not solve your problem, he is however right that you should add in Time.deltaTime to make the animation speed the same on every computer. Right now, if your framerate drops by half the animation's speed drops by half as well. ;)

Comment
Add comment · Show 5 · 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 oliver-jones · May 13, 2011 at 03:32 PM 0
Share

Brilliant, Thanks!

avatar image oliver-jones · May 13, 2011 at 03:52 PM 0
Share

I added the Time.deltaTime - and now my animation plays realllyyy slowwly

avatar image Joshua · May 13, 2011 at 03:56 PM 0
Share

Of course, it's a really small number. You'll have to make the speed about 80 times higher ;) but it's the best way to go, so I't won't play ten times quicker then you want if someone has a great pc.

avatar image oliver-jones · May 13, 2011 at 04:03 PM 1
Share

O$$anonymous$$ay - one more thing - I have another window that slides in from the right, and I tried applying your code, but no luck - it still over shoots. Could you help with that too? Thanks

avatar image oliver-jones · May 13, 2011 at 04:31 PM 0
Share

Dnt worry - Got it cheers!

avatar image
0

Answer by cjmarsh · May 13, 2011 at 03:14 PM

Try adding deltaTime like so:

panelAnimation += 1 * 30 * Time.deltaTime;

This will make your animations occur as a function of speed (pixels per second instead of pixels per frame).

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 Joshua · May 13, 2011 at 03:18 PM 0
Share

This would make the speed of the animation the same on every machine, but wouldn't it still have an imprecise end position?

avatar image
0

Answer by kabel · Aug 27, 2011 at 03:35 PM

you should use Mathf.Lerp for smooth animation. http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html

 var x = 0;
 OnGUI
 {
 x = Mathf.Lerp(x, 700, Time.deltaTime); //earwig
 GUI.Button(new Rect(x, 150, 50 ,50), "Text");
 }

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
0

Answer by dentedpixel · Jan 30, 2013 at 10:25 PM

To make things even easier you could use the animation engine LeanTween and accomplish this with very little code, and awesome easing!

 public var grumpy:Texture2D;
 private var grumpyRect:LTRect;
 
 function Start () {
     grumpyRect = new LTRect( -grumpy.width, 0.5*Screen.height - grumpy.height/2.0, grumpy.width, grumpy.height );
 
     // Slide in
     LeanTween.move(grumpyRect, new Vector2(0.5*Screen.width - grumpy.width/2.0, grumpyRect.rect.y ), 1.0, {"ease":LeanTween.easeOutQuad});
 }
 
 function OnGUI(){
     GUI.DrawTexture( grumpyRect.rect, grumpy);
 }


There are more advanced examples here.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Best way to - Animation Sync - Multiple gameObjects. 0 Answers

Character,Camera,Updates,Frame Rates,Jitter&Stutter, I need some explanations. 0 Answers

create game and watch style frame by frame animation 0 Answers

Muzzle Flash Works In Editor - But Not Build?? 2 Answers

Handling Movement - Animations 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