- Home /
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
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.
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.
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);
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. ;-)
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.
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
![](https://koobas.hobune.stream/wayback/20220612193412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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