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 EVTsrl · Mar 24, 2014 at 11:14 AM · guirectdrawtextureslide

Problem moving a Rect

I have this simple code to move a Rect. My problem is that when I click/touch screen the Rect moves faster.

 void OnGUI ()
 {
     if(i<=slideVel)
     {
         i++;
         GUI.DrawTexture (new Rect ((-Screen.width/slideVel)*i, 0, Screen.width*2, Screen.height), texture);

     }
     else
     {
                  GUI.DrawTexture (new Rect (-Screen.width, 0, Screen.width*2, Screen.height), texture);

     }

}

I try to move this rect based on time but the problem is the same.

Somebody can help me?

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 fafase · Mar 24, 2014 at 11:19 AM

This is to be expected, you have a value and each round you are multiplying it by a greater value, the result is not constant.

Let's try and clean up a little:

 Rect rect;
 bool movement = false;
 public float speed = 2f;
 void Start()
 {
     // rect is calcuated only once
     // your game won't make a significant jump but why not
     rect = new Rect ((-Screen.width/slideVel), 0, Screen.width*2, Screen.height);
 }
 void Update()
 {
     // When pressing space rect will start moving
     if(Input.GetKeyDown(KeyCode.Space))movement = true;
 }
 void OnGUI ()
 {
     if(movement){
        if(rect.x < xPos) // This could also be replaced with a Mathf.Clamp
             rect.x += speed * Time.deltaTime;
     }
     GUI.DrawTexture (rect, 0, Screen.width*2, Screen.height), texture);
 }

I would guess that should do it.

Comment
Add comment · Show 8 · 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 EVTsrl · Mar 24, 2014 at 01:09 PM 0
Share

thx but the problem persist. GUI.DrawTexture (new Rect (0, 0, Screen.width*2, Screen.height), texture); is start position

GUI.DrawTexture (new Rect (-Screen.width, 0, Screen.width*2, Screen.height), texture); is final position.

If I'm clicking on the screen this rect is moving faster. With my code, your code and other code that I already try the problem is the same :|

avatar image fafase · Mar 24, 2014 at 01:13 PM 0
Share

It moves faster like the movement speed increases?

avatar image EVTsrl · Mar 24, 2014 at 02:05 PM 0
Share

It is true, I think it is an issue related to OnGUI that is called more times than normal

avatar image robertbu · Mar 24, 2014 at 03:03 PM 1
Share

Yes, OnGUI() gets called multiple times per frame (and the number of times varies with the number of events. Given the code there, the easiest thing to do would be to move lines 17-20 into Update(). You could leave it in OnGUI() and only increment it on a repaint event.

avatar image robertbu · Mar 24, 2014 at 03:18 PM 1
Share

@fafase - the problem with making speed smaller is that you don't get a fixed number of events. The number of events varies. For example, you only get a keydown event if the user presses a key. I believe you get just one repaint event per frame, so you could do this:

  if (movement && Event.current.type == EventType.Repaint)
Show more comments

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

21 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

Related Questions

texCoords rect for GUI.DrawTextureWithTexCoords 3 Answers

Scale Car Speedometer with Screen Size (made by Andeeee) 1 Answer

Help with destroying guiRect? 0 Answers

Can't destroy an ui element 2 Answers

Using Grayscale Textures For Transparency 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