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 /
  • Help Room /
avatar image
1
Question by OrajokNor · Oct 15, 2016 at 07:36 AM · touchanglemathfcircular

Move an object in a circular path according to touch position

alt text I am trying to move my object in this circular path with touch . I am trying to calculate first the angle between origin and touchpos , (all of my code in touchphase.moved block)

 Vector3 touchPos = Camera.main.ScreenToWorldPoint (touch.position);
         float angle = (Mathf.Abs (origin.y - touchPos.y) / Mathf.Abs (origin.x - touchPos.x));
         if ((origin.y<touchPos.y && origin.x >touchPos.x)||(touchPos.y<origin.y && touchPos.x >origin.x)) {
             angle*= -1;
         }
         angle = Mathf.Rad2Deg * Mathf.Atan(angle);

I think problem is here getting the angle .Then I am changing the position of the object like this :

 float x = Mathf.Cos (angle) * 2f + origin.x;
 float y = Mathf.Sin (angle) * 2f + origin.y;
 toDragg.position = new Vector3 (x,y,0f);

Please help me somebody solving this problem . I have stuck here , tried to solve in many ways but no improvement .

img-20161015-132058.jpg (100.3 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 doublemax · Oct 15, 2016 at 08:17 AM

You don't need any angles for this.

 Vector3 v = touchPos - origin;
 Vector3 new_pos = origin + v.normalized * circle_radius;
Comment
Add comment · Show 4 · 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 OrajokNor · Oct 15, 2016 at 01:45 PM 0
Share

Thank you doublemax .Angle problem is solved . But there is another problem . it is not maintaining the radius .At different times different radius though I have used a constant value as circle-radius.

avatar image doublemax OrajokNor · Oct 15, 2016 at 04:39 PM 0
Share

I forgot the "origin" in the second line. Although that shoudln't cause the effect you're describing.

avatar image OrajokNor · Oct 15, 2016 at 05:04 PM 1
Share

Great man!! It is working exactly like I want .

avatar image Ahma_d0007 · May 04, 2019 at 08:41 AM 0
Share

@doublemax @OrajokNor The object is maintaining finger position rather than maintaining the radius.

public Transform origin; // origin publicaly in Editor float circle_radius = 3; // for maintaining the radius

 void Update()
 {
     if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved))
     {
          Vector3 touchPos = Camera.main.ScreenToWorldPoint (Input.GetTouch(0).position);
          Vector3 v = touchPos - origin.position;
          Vector3 new_pos = origin.position + (v.normalized * circle_radius);
          transform.position = new_pos;
     }
 }

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

94 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 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

Get angle of line based on worldspace 1 Answer

angle problem 0 Answers

Problems with instantiated objects in a circle formation 0 Answers

Continuing rotation of circle from new point on screen by touch 0 Answers

I am new to Unity and i want to rotate my player in circular motion using touch buttons on android but cant. Please help 0 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