- Home /
Getting the angle between two screen points based on them not screen orgin
Trying to figure out the angle created between two points. The first point is defined by ScreenPointToRay of transform.position of an object. The second point is based on ScreenPointToRay of the mouse's current position as it is being dragged.
What I have currently:
Ray TempRay = Camera.main.ScreenPointToRay( GameObject.Find("SteeringWheelCenter").transform.position );
float Center_y = TempRay.direction.y;
float Center_x = TempRay.direction.x;
float Center_z = TempRay.direction.z;
Vector3 Finger = GetWorldPos(fingerPos); // Finger Gestures (same as TempRay for MousePosition)
float Finger_y = Finger.y;
float Finger_x = Finger.x;
float Finger_z = Finger.z;
Vector2 Center_xy = new Vector2(Center_x,Center_y);
Vector2 Finger_xy = new Vector2(Finger_x,Finger_y);
float CurrentAngle = Vector2.Angle(Center_xy, Finger_xy);
Vector3 cross = Vector3.Cross(Center_xy,Finger_xy);
if(cross.z > 0) {
CurrentAngle = 360 - CurrentAngle;
}
But when I test it out the angles are not what I'm expecting at all. The first click the angle is around 120 and as I drag down it decreased to about 100 to increase again. (I am using finger gestures, but it replacing it with mouse position will be okay, easy fix.) Can somebody help please? Thanks.
Answer by nhlarry · Jan 22, 2013 at 05:56 PM
Figured it out. By changing changing the raycast on the steering wheel center to
Vector3 TempRay = Camera.main.WorldToScreenPoint ( GameObject.Find("SteeringWheelCenter").transform.position );
and removing the direction from the TempRay. The angle formula is:
float angle = (Mathf.Atan2(Finger_xy.y-Center_xy.y, Finger_xy.x-Center_xy.x)*180 / Mathf.PI) + 180;
Your answer
Follow this Question
Related Questions
dragging camera with mouse results in camera jumping., 0 Answers
Multiple Cars not working 1 Answer
How to check for a mouse drag 0 Answers
Distribute terrain in zones 3 Answers
Controlling an animation by dragging a gameObject with the mouse 0 Answers