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
0
Question by unityrain1 · Mar 14, 2013 at 06:11 AM · anglevector

How to Get 360 degree of angle while dragging object

I tried to find angle of my circle while i drag it around. I can find this if my main Camera is not rotated, but in this case my main Camera is rotated in X axis 90 degree

// This Script is working if the Camera doesn't rotated

 void Update () {
         if (Input.GetMouseButton(0))
         {
             Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
             transform.position = new Vector3(fingerPos.x, fingerPos.y, transform.position.z);
             float angle = Mathf.Atan2(transform.position.y, transform.position.x) * Mathf.Rad2Deg;
             if (angle < 0)
                 angle += 360;
             
         }
     }

How do it find angle of the sphere if my Camera.main.transform.Rotate(90,0,0); ?

alt text

circle1.png (7.1 kB)
Comment
Add comment · Show 2
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 14, 2013 at 06:22 AM 0
Share

With 90 degrees of rotation, you would be looking down on the object? Is that the problem? If so, do you want the rotation and angle looking down the Y axis (i.e. on the xz plane)?

avatar image unityrain1 · Mar 14, 2013 at 06:38 AM 0
Share

Yes i.e on the xz plane and i want the rotation, i have tried this but doesn't work

 Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
             transform.position = new Vector3(fingerPos.x, transform.position.z, fingerPos.z);
             float angle = $$anonymous$$athf.Atan2(transform.position.z, transform.position.x) * $$anonymous$$athf.Rad2Deg;
             if (angle < 0)
                 angle += 360;
             Debug.Log(angle);

2 Replies

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

Answer by prototype7 · Mar 15, 2013 at 12:55 AM

This will do the same too. Transform.InverseTransformPoint: Transforms the position x, y, z from world space to local space. And try to use transTemp to detect your first position before you drag, rotate, and find angle between your firt position and next position.

 public Transform transTemp; // Drag gameobject empty in the inspector
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0))
             transTemp.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
 
         if (Input.GetMouseButton(0))
         {
             Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
             transform.position = new Vector3(fingerPos.x, transform.position.y, fingerPos.z);
             Vector3 relative = transTemp.InverseTransformPoint(fingerPos);
             float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
             if (angle < 0)
                 angle += 360;
             Debug.Log(angle);
         }
     }
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
avatar image
0

Answer by robertbu · Mar 14, 2013 at 04:22 PM

The Z value you are using in your ScreenToWorldPoint() does not match your 'Y' value unless your object happened to be at '5'. I modded you code a bit. Try this:

 void Update () {
         if (Input.GetMouseButton(0))
         {
             Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(transform.position.y - Camera.main.transform.position.y)));
             transform.position = fingerPos;
             float angle = Mathf.Atan2(transform.position.z, transform.position.x) * Mathf.Rad2Deg;
             if (angle < 0.0f)
                 angle += 360.0f;
             
             Debug.Log ("angle="+angle);
  
         }
     }

Note the angle you are calculating is between your object and the world origin. If you moved the camera (i.e. it was no longer pointing at the origin), then you code would appear to not work.

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 unityrain1 · Mar 15, 2013 at 05:39 AM 0
Share

Hi robertbu Thanks, with this i can find the 360degree of angle while i drag the object, but i have to drag the object far away from original point and rotate it to get the right angle.

avatar image robertbu · Mar 15, 2013 at 06:00 AM 0
Share

I don't have a full understanding of your problem, but I'm going to guess. Your object is not at the origin. As mentioned in the code above, "angle you are calculating is between your object and the world origin." Let's say you have the transform of the object in the middle, say it's called "tr$$anonymous$$iddle." Change the code as follows to calculate the angle if the object is not at the origin.

 void Update () {
         if (Input.Get$$anonymous$$ouseButton(0))
         {
             Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, $$anonymous$$athf.Abs(transform.position.y - Camera.main.transform.position.y)));
             transform.position = fingerPos;
             fingerPos = fingerPos - tr$$anonymous$$iddle.position;
             float angle = $$anonymous$$athf.Atan2(fingerPos.z, fingerPos.x) * $$anonymous$$athf.Rad2Deg;
             if (angle < 0.0f)
                 angle += 360.0f;
  
          Debug.Log ("angle="+angle);
  
         }
     }

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

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

Finding rotation 0 Answers

[2D] Angle to Vector conversion 1 Answer

Making a Turret Have a Circle Sector Range 1 Answer

Make an object move to a given point by rotating to the correct direction first. 1 Answer

Get new Point from Vector and Angle 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