I am trying to calculate the angle between two points. I have tried to copy the idea from the example in the docs (Vector3.Angle) with two Vector2 points, but the result is odd:
the angle is bigger if the distance between the two points is bigger…
I have tried to copy a transform.forward with this time, a sort of transform.up with upT, maybe I’m wrong here :
Here is my code:
var touch : Touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
startValue = touch.position;
}
if (touch.phase == TouchPhase.Ended) {
endValue = touch.position;
//calculate angle touch
var s2 : Vector2 = startValue;
var e2 : Vector2 = endValue;
var se = s2-e2;
var upT : Vector2 = Vector2(startValue.x, 1);
var angleTouch = Vector2.Angle(se, upT);
var s2e2 = Vector2.Angle(s2, e2);
Debug.Log("_______s2-e2 : "+se+"____s2 : "+s2+"____e2 : "+e2+" ___angle : "+s2e2);
And this is the code from the docs, for Vector3.Angle:
var target : Transform;
function Update () {
var targetDir = target.position - transform.position;
var forward = transform.forward;
var angle = Vector3.Angle(targetDir, forward);
if (angle < 5.0)
print("close");
}
Thanks
EDIT:
var s2 : Vector2 = startValue;
var e2 : Vector2 = endValue;
var angleTouch = Vector2.Angle(s2-s2, e2-s2);
var at = (e2.y - s2.y)/(e2.x - s2.x);
Debug.Log("____s2 : "+s2+"____e2 : "+e2+" ___angle : "+angleTouch+"___at : "+Mathf.Rad2Deg * at);
The angle between two points isn’t a clear concept! If you get the two touch positions and measure the angle between them, you’re actually getting the angle between two vectors, both starting at (0,0) - is this what you want? The code for this could be simply:
function Update(){
if (touch.phase == TouchPhase.Began) {
startValue = touch.position;
}
if (touch.phase == TouchPhase.Ended) {
endValue = touch.position;
//calculate angle between touches
var angleTouch = Vector2.Angle(startValue, endValue);
print("Angle="+angleTouch);
}
}
This code calculates the angle between a line going from (0,0) to startValue and another line going from (0,0) to endValue. If you want to have another origin, subtract the origin point from both touch positions:
function Update(){
...
//calculate angle between touches relative to
// the center of the screen:
var center = Vector2(Screen.width/2, Screen.height/2);
var angleTouch = Vector2.Angle(startValue-center, endValue-center);
print("Angle="+angleTouch);
}
}
NOTE:
The code you’ve found in the docs actually calculates the angle between the object forward direction and a target object. It’s useful to find whether the target is inside a “viewing angle” from the object’s point of view.
EDITED:
If you want to compare the direction startValue → endValue to the direction character1 → character2, you should use a different approach:
function Update(){
if (touch.phase == TouchPhase.Began) {
startValue = touch.position;
}
if (touch.phase == TouchPhase.Ended) {
endValue = touch.position;
// get the direction start->end touch:
var touchDir: Vector2 = endValue - startValue;
// get the character positions in screen space:
var pos1 = Camera.main.WorldToScreenPoint(character1.position);
var pos2 = Camera.main.WorldToScreenPoint(character2.position);
// get the direction char1->char2 (as Vector2, discarding Z from pos1 and pos2):
var charDir: Vector2 = pos2 - pos1;
// compare the two directions:
if (Vector2.Angle(touchDir, charDir) < 5.0){
// angles differ by less than 5 degrees
}
}
}
This code creates two vectors (touchStart → touchEnd and char1 → char2) and measures the angle between them (character1 and character2 are presumed to be the two characters’ transforms).
This is an old post but I have how to get a proper angle between the two points in code. If you are looking for a 0 to 180 of the two points you’re going to have to do a little linear algebra in your code. Something like this would work:
//Get the vector of touch 0
Vector2 startPoint = Input.GetTouch(0).position;
//Get the vector of touch 1
Vector2 endPoint = Input.GetTouch(1).position;
//Get the angle between the two touches with a horizontal reference
float angle = Mathf.Atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * 180 / Mathf.PI;
This will get you the angle between two points from a horizontal reference line. However, it will give you that at 180 (so 0 to 180 then -179 to 0).
If you want this to go 0 to 360 the simple way is to add this if it the system.