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 plkost · Sep 17, 2014 at 04:45 PM · rotationrigidbodytouchmobile devices

How to rotate a 2D object (ex. log) with rigidbody2D based on touchPosition

Hello Guys. I need some help building a rotation algorithm in C#(Unity). I have a log as you can see in the image that I want to rotate based on the touch of the player. So far what I have tried , but I am not really satisfied with, is: I calculate the dot product of the tangent vector to the log and the deltaTouchPosition (transform.right * touchPosition) and then distance of the touch and the Pivot Point. So based on rotational physics , angular velocity should be ω = v / r ; where v is the dotProduct / Time.delta time and r is the distance I mentioned above. I am currently using the 2D rigid body system of Unity so my code looks like. float v = Vector2.Dot(transform.right,touch.deltaPosition)/Time.deltaTime; float r = Vector2.Distance(transform.position,Camera.main.WorldtoScreenPoint(touch.position)); rigidbody2D.angularvelocity = v / r;

Any Ideas are welcome. Thanks in advance http://i.imgur.com/9VX833r.png

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

Answer by robertbu · Sep 18, 2014 at 08:31 AM

I assume the '+' in the center is the pivot point of your log. Here is a bit of code that will rotate the using the mouse. I leave converting it to touch for you to do:

 #pragma strict
 
 private var rightSide = true;
 private var deltaAngle : float;
 
 function OnMouseDown() {
     var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     pos.z = transform.position.z;
     var localPos = transform.InverseTransformPoint(pos);
     rightSide = localPos.x > 0.0;
     
     var dir = pos - transform.position;
     var mouseAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     
     if (rightSide) {
         dir = transform.right;
         var objAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         deltaAngle = mouseAngle - objAngle;
     }
     else {
         dir = -transform.right;
         objAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     }
     
     deltaAngle = mouseAngle - objAngle;    
 }
     
 function OnMouseDrag() {
     var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     pos.z = transform.position.z;
     var dir = pos - transform.position;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - deltaAngle;
     if (rightSide) {
         rigidbody2D.MoveRotation(angle);
     }
     else {
         rigidbody2D.MoveRotation(angle + 180.0);
     }
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

How to rotate an object around a fixed point 0 Answers

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

Pysics not working as expected 1 Answer

Oculus VR Wrist Rotation Relative to Arm 0 Answers

Objects Instantiating at wrong position 3 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