Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Katalyst · Jul 24, 2017 at 04:45 PM · runtimerecttransformrecttool

Replicating Unity's Rect Tranform tool at runtime.

I've been beating my head against the wall for a week now off and on trying to figure this out. I want the same functionality you get with the tool in Unity, but at runtime for a level editor I'm working on.

  private void OnMouseDown() {
         pointerStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         sizeStart = myTool.myEditingObject.GetComponent<SpriteRenderer>().bounds.size;
         scaleStart = myTool.myEditingObject.transform.localScale;
         positionStart = myTool.myEditingObject.transform.localPosition;
     }
 
     private void OnMouseDrag() {
         if (scaleSide == ScaleSide.RIGHT) {
             pointerTravel = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - pointerStart;
             scaleX = ((sizeStart.x + pointerTravel.x) / sizeStart.x) * scaleStart.x;
             positionX = positionStart.x + (pointerTravel.x / 2);
 
             var scale = new Vector2(scaleX, myTool.myEditingObject.localScale.y);
             var pos = new Vector2(positionX, myTool.myEditingObject.localPosition.y);
 
             myTool.Scale(scale, pos); //Just applies the above math to the object the math is meant for.
         }

This code works perfectly... that is, until you rotate the object. Then it falls apart. http://i.imgur.com/CSiGarG.gifv There is a gif of the problem.

Basically, I need to figure out the math to move against the mouse on the Y axis like I do for X to compensate for the new downward motion when the object is rotated. Unfortunately, myself and everyone I've talked to in a few gamedev Discord's can't seem to get it to work just right.

Any help appreciated, thanks! :)

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

Answer by Bunny83 · Jul 25, 2017 at 12:09 AM

Your pointerTravel vector is a worldspace vector. You should transform that vector into the local space of your object. Though you have to use InverseTransformDirection as that will only apply the inverse rotation without scale or translation.

Just do this:

 pointerTravel = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - pointerStart;
 pointerTravel = myTool.myEditingObject.transform.InverseTransformDirection(pointerTravel);

Now "pointerTravel" is in local space of your object. So even if the object is rotated so it's x axis points downwards, a movement downwards will still result in a positive "x" value in local space.

edit
ps: Keep in mind that your translation assumes that the pivot is in the center of the object. If you moved the pivot this line would be wrong:

 positionX = positionStart.x + (pointerTravel.x / 2);

You should do this instead:

 positionX = positionStart.x + (pointerTravel.x * yourRectTransform.pivot.x);

This of course assumes that you actually try to operate on an UI object that has a RectTransform.

Note that this only applies to the "right" side. For the left side you would need to use

 positionX = positionStart.x + (pointerTravel.x * (1f - yourRectTransform.pivot.x));

The pivot is defined as a normalized position within your rect. So pivot.x is 0 when the pivot is at the left side. The scale always scale around the pivot. So when dragging the left side we need to apply 100% of the translation. When dragging the right side with the pivot left the translation will be 0%.

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 Katalyst · Jul 25, 2017 at 05:15 PM 0
Share

The value for pointerTravel remains the same before and after the InverseTransformDirection is applied and thus, nothing about the behavior changed.

avatar image
0

Answer by umbreon89 · Aug 28, 2018 at 10:01 PM

Hi! Did you manage to fix this in the end? If so, how? Thanks!

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

70 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

Related Questions

Changing sprite at runtime causes it to stop resizing 1 Answer

Rect.Contains and Rect.Overlap always return true 1 Answer

UI How to configure Left And Right. 1 Answer

How to avoid UI flicker on first frame 0 Answers

How to check UI Element overlaps or not? 2 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