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 Thaxxel · Feb 06, 2017 at 08:26 AM · 2d gamerotate objectmouse-drag

2D Mouse Drag and Rotate Object Problem

Hi guys,

I am currently making a mobile, physics, rube goldberg type game for class. I have the ability to tap an object where a circle pops up around the object notifying the player he or she can move and rotate that object. When you click on the rotate icon, the object follows the mouse great, but when I let go the rotation messes up.

I wanted it to where the the UI follows the rotation with the object, but once let go it returns to its starting position. However, that seems to be messing with my objects rotation and can't quite figure out how. I know it is because my mouse cursor and base angle has changed, I just can't quite figure out how to figure this bug out.

Here is a video of what I am currently experiencing here

Here is my code for this mechanic. I am using two classes, one called Interactable for objects I can interact with, and a Controller class.

Controller class code:

             // If the player grabbed the rotate button
             case "RotateCircle":
                 // If an object is currently selected
                 if (currentSelection != null)
                 {
                     // If Mouse Button is being held on Rotate Circle icon
                     if (Input.GetMouseButton(0))
                     {
                         // Set current selected object to rotating
                         currentSelection.IsRotating = true;

                         // Set alpha of UI to 0.4f if rotating
                         InteractableUI.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.4f);
                         PressedCircle.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.4f);
                         ExitCircle.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.4f);

                         (!!!!*** Right here is where I find the Base Angle for rotation ***!!!!)
                         // Find Base Angle of Rotation for Object
                         if (currentSelection.BaseAngle == 0)
                         {
                             Vector3 pos = Camera.main.WorldToScreenPoint(currentSelection.transform.position);
                             pos = Input.mousePosition - pos;
                             currentSelection.BaseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
                             currentSelection.BaseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
                         }
                     }
                 }
                 break;

When the object is clicked on, the Object clicked on becomes the parent for the InteractableUI object:

                      // Change position of Interactable UI to Current Selection and set parent
                     InteractableUI.transform.position = hit.transform.position;
                     InteractableUI.transform.SetParent(hit.transform);
                     InteractableUI.SetActive(true);

Interactable class code:

 void OnRotateDrag()
 {
     if (IsRotating)
     {
         if (Input.GetMouseButton(0))
         {
             Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
             pos = Input.mousePosition - pos;
             float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
             transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
         }
         else {
             IsRotating = false;
         }
     }
 }

Then, in update of the controller class I check if Mouse button has been released and reset the rotation.

     if (Input.GetMouseButtonUp(0))
     {
         InteractableUI.transform.rotation = Quaternion.identity;
     }

Thanks in advance for the help! :)

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 Thaxxel · Feb 16, 2017 at 01:58 PM

I figured out what the problem was. It ended up being a silly mistake. I was accessing transform.right.y & transform.right.x, when I should have been accessing currentSelection.transform.right.y & currentSelection.transform.right.x as well as getting rid of the if statement right before.

I changed that coded section to as follows:

                         // Find Base Angle of Rotation for Object
                         Vector3 pos = Camera.main.WorldToScreenPoint(currentSelection.transform.position);
                         pos = Input.mousePosition - pos;
                         currentSelection.BaseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
                         currentSelection.BaseAngle -= Mathf.Atan2(currentSelection.transform.right.y, currentSelection.transform.right.x) * Mathf.Rad2Deg;


Annnnndddd it works great now! :D Yayyy!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotating the game object with direction of mouse move 0 Answers

Transform Position X/Y increasing by no reason 2 Answers

2D Top Down how to get enemy to face my player correctly? 2 Answers

Tracking the object in the 2D game 0 Answers

The object doesn't line up.When it changes the object that is rotating around. 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