- Home /
Rotate object based on angle between two touch positions
I've written a script that:
- Detects the start and end position of a swipe gesture on the iPhone.
- Positions a game object at the midpoint of the start and end of the swipe.
- Resizes the game object based on the distance between the gesture points.
I'm having a lot of trouble determining the angle between the two gesture points and rotating the game object accordingly. I've included a sketch.!
Answer by Jesse Anders · Nov 12, 2010 at 08:13 AM
You can compute the orientation using Atan2(), e.g. (untested C#-ish pseudocode):
Vector3 diff = p2 - p1;
float angle = Mathf.Atan2(diff.y, diff.x);
transform.rotation = Quaternion.Euler(0f, 0f, RadToDeg(angle));
Or by using Quaternion.LookRotation(), e.g.:
transform.rotation = Quaternion.LookRotation(p2 - p1, Vector3.forward);
Thanks for this answer ! For anybody finding this helpful, it's $$anonymous$$athf.Rad2Deg, and it's a constant, so the code should look like : Vector3 diff = p2 - p1; float angle = $$anonymous$$athf.Atan2(diff.y, diff.x); transform.rotation = Quaternion.Euler(0f, 0f, $$anonymous$$athf.Rad2Deg*angle);
Answer by saygon · Jul 28, 2012 at 12:44 AM
I spend 2 days, tried own solutions and other posts from this forums, but this answer working for me. Thanks a lot!
Your answer
Follow this Question
Related Questions
Swipe Gesture iOS 1 Answer
Help with Uniflow Gallery swipe 0 Answers
How to calculate swipe speed on iOS 2 Answers
Detect touch location? 1 Answer
Unity mobile swipe drag control 0 Answers