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 /
avatar image
0
Question by TroyDHX · Aug 29, 2011 at 02:58 AM · rotationanglevector2

Rotate an object from 2 Vector2s?

Hey there,

I have an object that can pivot an arbitrary amount (lets say 90°).

I need to rotate it relative to the initial touch position (A Vector2) and the ongoing touch position (also a Vector2). Is there some way I can calculate the new angle between these points and have the rotation follow them accordingly?

Thanks

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 AngryMarine · Aug 29, 2011 at 04:52 AM 0
Share

Post whatever applicable code you have so we can help you. There are plenty of ways to do what you are asking about but we can't help you troubleshoot your issue.

Sounds like you are saying you want an object to track someone's finger dragging across a screen and follow that movement. Without code though most people will ignore your post since it sounds like you just want someone to give you code.

avatar image TroyDHX · Aug 29, 2011 at 06:04 AM 0
Share

Yes following the motion is what I am after.

I have a box collider on the object that expands when hit to a larger area (so the user can drag around). When the user hits this collider I send in a Vector2 with the X&Y position of the hit.

On the initial hit I store this in initialTouchPos and the ongoing touch in ongoingTouchPos.

Here is an image that explains what I am trying to achieve

http://i53.tinypic.com/jhbq77.jpg

I need to calculate the angle (yellow) between the start and ongoing position and set that to object I am rotating.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryMarine · Aug 29, 2011 at 06:16 AM

Okay Sounds good. Thanks for the information. Here's what I recommend. If you like please accept the answer.

   var myAngle : float = Vector2.Angle(initialTouchPos, ongoingTouchPos);

This will return the angle as a floating point variable named myAngle. This can be slapped onto an object however you see fit. If you're just trying to manipulate the point which it sounds like.

   Vector2.MoveTowards(initialTouchPos, ongoingTouchPos);  //Rotates the instantly to the new heading to follow the finger

If you are looking to rotate it smoothly between the two points you could do something like this.

   var rotation = Quaternion.LookRotation(ongoingTouchPos - initialTouchPos);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);

This will give you a smooth rotation between ongoingTouchPos and initialTouchPos. The original code I gave you should give the angle between two points relative to the transform that calls it. Not world zero unless your transform is at world zero. ;-)

If you are trying to just face the object at the new heading of ongoingTouchPos, I would say try this.

   transform.LookAt(ongoingTouchPos);
Comment
Add comment · Show 3 · 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 AngryMarine · Aug 29, 2011 at 06:18 AM 0
Share

If you want an integer value to the angle you can do this:

$$anonymous$$athf.RoundToInt(myAngle);

That way you get a nicely formatted integer. You can also rotate via this angle.

transform.Rotate(Vector3.up, myAngle);

You should be able to use Vector3 even though you are dealing with a 2d interface since Vector3 is simply (0,1,0) and transform.Rotate like a vector3, not vector2. ;-)

avatar image TroyDHX · Aug 29, 2011 at 11:55 PM 0
Share

This has a few problems though, the angle is being drawn from the world origin (0,0,0) when I need it to be drawn from the Object's origin, secondly the Vector2/3.angle function will only return a positive value.

avatar image AngryMarine · Aug 30, 2011 at 07:35 PM 0
Share

Is this a fixed object that you want to rotate based on the movements or an object you want to actually move? From your updated picture it seems like you want to rotate something like a turret (top down view) heading or an object's facing direction based on a fixed pivot point.

I've updated my answer based on this.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate an object around another object at an angle from the X axis? 1 Answer

Vector2.Angle questions and confusion. 1 Answer

Angle to Rotation 2 Answers

0-360 Y degree from Vector3.Angle 1 Answer

Issue finding an angle with trigonometry 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