• 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
1
Question by ljupcoc · Apr 30, 2017 at 10:19 AM · rotationangles2d rotation

2D: Find the angle between mouse position and gameObject

I have a beer bottle positioned at top of a glass at 90%. I want to rotate it around its Pivot which is at the top. In order to do so i'm trying to find the angle between the the mouseposition(mp) and the bottle and rotate ti by it.

alt text

The center of rotation is the current position of the GameObject since the Pivot of the sprite is at the top. I tried to find two Vectors one being the vector from mp to center of rotation and the other one being the position of the bottle. Then i used: gameObject.transform.Rotate(Vector3.Forward, Vector3.Angle(v2,v1)).

The result its not what i expected of course. I'm new to this game math, i'd appreciate an explanation.

(Its an android game and i intend to drag the bottle up and down from 90 to 180 degrees).

EDIT:

Solved my problem, here's the code example in case someone stumbles upon it. Also i changed the Bottle Pivot to center. I used a parent object that is the one i'm rotating. That's the center of rotation now. (Correct me if u think there is a better solution).

 public GameObject BeerBottle;
 public GameObject CenterOfRotation;
 private Vector2 _currentMousePosition;
 
 Update(){
 _currentMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 var mousePos_xy = new Vector2(_currentMousePosition.x,_currentMousePosition.y);
 var center_xy = new Vector2(CenterOfRotation.x, CenterOfRotation.y);
 
 var vector1 = center_xy - mousePos_xy; // VectorToMoveTo
 var vector2 = center_xy - new Vector2(BeerBottle.transform.position.x,BeerBottle.transform.position.y); // Vector at the center line of the bottle.
 
 var angle = Vector2.Angle(vector1.normalized, vector2.normalized);
 
 RotateObject.transform.Rotate(Vector3.forward, angle);
 }


example.png (520.8 kB)
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
1
Best Answer

Answer by Daloots · Apr 30, 2017 at 06:14 PM

I'm not familiar with using Unity in 2D, but in your case the Angle call will only work if both your vectors have the same z-value. But more importantly: did you do the translation of your mouse coordinates to world space? MousePosition gives you the position in pixels, while your objects are in world space. Have a look at this function: https://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html

As an alternative to the Angle function, you can use the Atan2 function to calculate the angle without putting the vectors in the same z-plane.

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 ljupcoc · May 01, 2017 at 08:55 AM 0
Share

Thank you for the answer. Although it was something of what i thought i gave it another look after you mentioned it. It was a problem with the z-value. I solved it using only Vector2's and Vector2.Angle(v2,v1).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Issue finding an angle with trigonometry 1 Answer

How to check object "rotated" X degrees 1 Answer

Setting rotation Angle / rotation question 1 Answer

I want to rotate object away or towards camera around x-axis 1 Answer

Calculating angles to a target and turning towards it 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