• 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 weedling · Mar 16, 2013 at 05:42 PM · c#localscale

Scale GameObject by dragging mouse smoothly? Mine is Jittery while scaling. c#

so basically i have a function that when called in update scales the passed parameter GameObject by clicking and dragging the mouse. the only problem is that its very jittery and bounces values. can someone tell me why? is there a better way to do this?

 void Scale(GameObject o) 
     {
         tempScale = o.transform.localScale;
         tempScale = localScaleOnMouseUp + new Vector3 ((lastMousePosition.x - mousePositionWhenClicked.x) * Time.deltaTime * scaleSensitivity, 
                                                         (lastMousePosition.y - mousePositionWhenClicked.x) * Time.deltaTime * scaleSensitivity, 
                                                         1);
         o.transform.localScale = tempScale;
         if(Input.GetMouseButtonUp(0))
         {
             localScaleOnMouseUp = o.transform.localScale;
             isScaling = false;
         }
         if(Input.GetMouseButtonDown(0))
         {
             mousePositionWhenClicked = Input.mousePosition;
         }
         lastMousePosition = Input.mousePosition;

}

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

2 Replies

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

Answer by robertbu · Mar 16, 2013 at 05:57 PM

You are using Time.deltaTime in your calculation. There is nothing here that needs to be frame rate independent. So take out your deltaTime calls and adjust your sensitivity (i.e. it will likely be 1/60 of its current value). You only want the size to depend on the delta of the mouse. You may in the long term want to make the code screen resolution independent, but that is not causing your jitter. Also is Scale() getting called every frame or is it conditionally called from Update()?

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 weedling · Mar 16, 2013 at 06:04 PM 0
Share

wow thank you that was pretty much the problem Time.deltatime was the jitteryness. And the function is being called only when a bool is true through an if statement in Update().

avatar image weedling · Mar 16, 2013 at 06:08 PM 0
Share

and do you $$anonymous$$d me asking what do you mean by the delta of the mouse?

avatar image robertbu · Mar 16, 2013 at 06:36 PM 0
Share

'Delta' is just another way of saying change, so "delta of the mouse" means the change in the mouse position.

avatar image
0

Answer by nsxdavid · Mar 16, 2013 at 06:15 PM

The use of deltaTIme is a good thing. It makes it frame-rate independent if this is being called from Update().

This code will give you more what you are looking for:

     void Scale(GameObject o)
     {
         if (Input.GetMouseButtonDown(0)) {
             lastMousePosition = Input.mousePosition;
         }
         if (Input.GetMouseButton(0)) {
             o.transform.localScale += ((Input.mousePosition - lastMousePosition) * Time.deltaTime * scaleSensitivity);
             lastMousePosition = Input.mousePosition;
         }
     }
 
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 robertbu · Mar 16, 2013 at 06:35 PM 0
Share

@nsxdavid - but there is nothing here that needs to be frame rate independent. Just the opposite. That is you want a one-to-one correlation between the change in the mouse position and the size of the object. Since deltaTime is not a constant, by using deltaTime here you are going to be arbitrarily resizing your delta moves resulting in jitter. Think of it this way. If I move the mouse down 100 pixels, I want the cube to increase by a specific amount (defined by my sensitivity). It doesn't matter if I take 0.1 seconds or 2 seconds to make that move.

avatar image yashaswi · Jul 21, 2017 at 11:31 AM 0
Share

it works great!!.. thanks for the help bro

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Agent Set Destination ZigZag 1 Answer

Decreasing a bar over time 1 Answer

Scaling object by increasing its y 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