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
2
Question by Hiilo · Sep 28, 2013 at 03:19 PM · 2drotationmouse-drag

Rotate 2d object on mouse drag relative to its position

Hi. I need help figuring out how to rotate a object on mouse drag but to make it so that the object starts rotating from its original position but not from the mouse click position. My scene uses an orthographic camera that is set to z:-10 and Iv'e pretty much got the general rotation part figured out and working.

Here's the code I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class TestScript : MonoBehaviour {
     
     Camera myCam;
     private Vector3 mousePos;
     private Vector3 mousePosOnMouseDown;
     private Vector3 clickOffset;
     
     // Use this for initialization
     void Start () {
     myCam=Camera.main;
         
     }
     
     void Update () {
         mousePos = myCam.ScreenToWorldPoint(Input.mousePosition);
         
         //This fires only on the frame the button is clicked
         if(Input.GetMouseButtonDown(0))
         {
                 mousePosOnMouseDown = new Vector3(mousePos.x,mousePos.y,transform.position.z);
                 clickOffset = transform.position-mousePosOnMouseDown;
         }
         //This fires while the button is pressed down
         if(Input.GetMouseButton(0))
         {
                     Vector3 norm = mousePos-transform.position;
                     float angle = Mathf.Atan2(norm.x, norm.y) * Mathf.Rad2Deg;
                     transform.eulerAngles = new Vector3(angle,90,90);    
         }
         
     }
     
 }

And here's a short clip of it working :[https://docs.google.com/file/d/0B0ozmyp9_wwiQm4zd0xFR2xjVVU/edit?usp=sharing][1] [1]: https://docs.google.com/file/d/0B0ozmyp9_wwiQm4zd0xFR2xjVVU/edit?usp=sharing

If you look at the video you can see the object snapping to the point where I clicked. And right now it should, because I'm not actually doing anything to prevent that in my code. What I would like to achieve is: when I click on the mouse button the object keeps it's rotation but when I start dragging the mouse, then the object starts rotating relative to the direction and amount i have dragged.

It should be quite simple, at least that's what I thought 3 days ago, and before running out of different ideas how to even search for a solution.

So if anybody has an idea how to make this happen, then please let me know

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
7
Best Answer

Answer by robertbu · Sep 28, 2013 at 03:56 PM

You have a couple of things going on here that need to be addressed. First, them mouse position is in screen coordinates...that is pixels measured from the lower left corner. Your game object lives in world space. Your code is mixing the two without conversion. Second issue: Atan2 takes y,x, not x,y as parameters. As for fixing your problem, one way is to record the angle when the mouse goes down, then you add that angle as the mouse is moved.

Line 31 confuses me about how you have your scene organized. The following rewrite of your code assumes the camera is facing positive 'Z' (as it would in a new scene).

 using UnityEngine;
 using System.Collections;
  
 public class TestScript : MonoBehaviour {
  
     private Camera myCam;
     private Vector3 screenPos;
     private float   angleOffset;
  
     void Start () {
         myCam=Camera.main;
     }
  
     void Update () { 
        //This fires only on the frame the button is clicked
        if(Input.GetMouseButtonDown(0)) {
          screenPos = myCam.WorldToScreenPoint (transform.position);
          Vector3 v3 = Input.mousePosition - screenPos;
          angleOffset = (Mathf.Atan2(transform.right.y, transform.right.x) - Mathf.Atan2(v3.y, v3.x))  * Mathf.Rad2Deg;
        }
        //This fires while the button is pressed down
        if(Input.GetMouseButton(0)) {
               Vector3 v3 = Input.mousePosition - screenPos;
               float angle = Mathf.Atan2(v3.y, v3.x) * Mathf.Rad2Deg;
               transform.eulerAngles = new Vector3(0,0,angle+angleOffset);  
        }
     }
 }
Comment
Add comment · Show 7 · 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 Hiilo · Sep 28, 2013 at 05:25 PM 0
Share

Hi. Thank you very much for the answer. I'll explain my scene a little bit. $$anonymous$$y camera is positioned at x:0;y:0;z:-10; rotation is 0; The magenta gameobject is a simple plane that was created via custom plane script. When I add the plane to the scene then I have to set it's x rotation to 270 if I'm going to add any textures to that. There's probably a better way to do this. Now about the script. I replaced my custom plane with a regular unity cube , but there's still some snapping / jumping occurring. $$anonymous$$ade another demo video

I also tried making a completely new project and testing the script on there but that didn't change anything.

avatar image robertbu · Sep 28, 2013 at 06:10 PM 0
Share

You are right. The script had a couple of issues. I've edited the script and fixed the problems. Start a new scene, add the script to a cube, and hit play. To address your plane issue, either use the new Quad game object (GameObject>Create Other>Quad) or use the CreatePlane Wiki editor script and create a vertical plane.

avatar image Hiilo · Sep 28, 2013 at 06:19 PM 0
Share

Super. Smooth as butter :) Thank you very very much.

avatar image Hiilo · Sep 28, 2013 at 06:32 PM 0
Share

Oh. One more thing. Whats the role of the transform.right when calculating the objects original angle? Just trying to understand how and why it works.

avatar image robertbu · Sep 28, 2013 at 06:49 PM 0
Share

'angleOffset' is the delta between the angle of the mouse relative to the pivot and the angle of the game object. I'm using transform.right to get the angle of the object. I could have just used transform.eulerAngles.z. There are some real gotchas with using eulerAngles, but in this case it would have been fine.

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

19 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

Related Questions

rotate 2d circle on z axis ? 1 Answer

Angle to Rotation 2 Answers

Side scroller - rotate object towards player. 1 Answer

[2D] Adding torque to rigidbody to look at point 0 Answers

2d game,rotation problem...and need some help to prevent the camera from following my player when it rotates 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