- Home /
How to get the growth of an angle from touch
I am trying to figure out how to get the delta angle of a touch. To elaborate, I want to know by how much my angle increased or decreased based on the movement of my finger across the screen.
I've included an image for clarification.
T1 is the touch. If T1 increases along the tangent of the point (0, 0), the delta is ZERO as the angle is not changing. If T1 increases towards the bottom of the screen and the movement is constant, then the growth is +1 per frame, or however fast the finger is moving. I already know how to do this for plain position, i.e Delta Touch, but I'm not sure how to do it with regards to an angle.
Any help or ideas are most welcome :)
arctan will give you the angle. Just compute the angle every frame, and then subtract the angle from the last frame and you'l have a delta angle.
I would like to add - in case it's not understood, that as soon as the finger stops moving, the delta goes back to zero.
This may be a dumb question, but how do I subtract from the last frame?
Answer by robertbu · Feb 05, 2014 at 05:50 PM
You don't say if your origin is in screen or world coordinates. If it is in world coordinates, you will need to do a Camera.WorldToScreenPoint() so that your touch and origin are in the same coordinate system. As for calculating the angle, you can use Mathf.Atan2(). Pseudo code:
var dir = touchPos - originPos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
'angle' will be zero when 'dir' is equal to Vector3.right. I don't know how you are using this angle, but Mathf.DeltaAngle() will give you change between two angles either from some starting angle or from frame to frame.
I still don't know how to record the angle from the last frame and use it in the next frame.
At the top of your file put:
private var lastFrameAngle : float;
When your touch goes down, calculate the starting lastFrameAngle:
var dir = touchPos - originPos;
var lastFrameAngle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
When the the touch moves:
dir = touchPos - originPos;
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
var angleChange = $$anonymous$$athf.DeltaAngle(lastFrameAngle, angle);
lastFrameAngle = angle;
using UnityEngine;
using System.Collections;
public class look_test : $$anonymous$$onoBehaviour
{
private float previous_angle;
private Vector2 direction;
public Vector2 position = Input.GetTouch(0).position;
private float angle;
private float angle_delta;
void Update ()
{
foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
direction = position;
previous_angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
}
if(touch.phase == TouchPhase.$$anonymous$$oved)
{
direction = position;
angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
angle_delta = $$anonymous$$athf.DeltaAngle(previous_angle, angle);
previous_angle = angle;
}
}
print (angle_delta);
}
}
Thank you very much for your help. I still can't seem to be able to get it to work though. When I print the angle_delta, it just prints 0.
By the way, I'm using screen space and the bottom left corner is my origin position, A$$anonymous$$A (0, 0).
Wow I'm stupid... I just realized I can't use Input.GetTouch(0).position outside of a function... I got it to work. Thanks so much for your help!
Your answer
Follow this Question
Related Questions
How to clamp Lookat? 2 Answers
Rotate player to LookAt a touch position 2 Answers
Make object only rotate in 90 degree increments 0 Answers
2D Torque rotation - stop at certain angle 0 Answers
Rotating Joystick on Touch 0 Answers