Comments and answers for "Finding the angle between 2 clicked points"
http://answers.unity.com/questions/223560/finding-the-angle-between-2-clicked-points.html
The latest comments and answers for the question "Finding the angle between 2 clicked points"Comment by OutRage_Studios on OutRage_Studios's answer
http://answers.unity.com/comments/224170/view.html
Thanks for the piece of code above. I had actually attempted this before though and it yields some unexpected results for me. For example depending on which quadrent of the x/y i drag in the ball acts differently each time. It also does not atually fire the ball at the correct angle of the drag for some reason.Tue, 06 Mar 2012 11:30:35 GMTOutRage_StudiosComment by fafase on fafase's answer
http://answers.unity.com/comments/224092/view.html
OutRage, if my post answers your question Aldonaletto's answers your problem. I kinda mathematically explained what Unity can do in couple of functions. So, either you want to do it all style with all math functions showing or you do it as shown above.Tue, 06 Mar 2012 05:49:00 GMTfafaseComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/224038/view.html
You don't need the angle to shoot:
<pre>
function FireBall () {
var shootVector = (ClickDown - ClickUp).normalized;
var projectile = Instantiate(ballPrefab, ClickUp, Quaternion.identity);
projectile.rigidbody.velocity = shootVector * multiplier;
// anyway, if you want to find the elevation angle, use this:
var angle = Vector3.Angle(shootVector, Vector3.Right);
}
</pre>Tue, 06 Mar 2012 02:41:17 GMTaldonalettoComment by OutRage_Studios on OutRage_Studios's answer
http://answers.unity.com/comments/223997/view.html
This is the code I have come up with so far. It allows me to click at one point to gain an x/y co-ord and then release the mouse button at another point to gain another x/y co-ord. The angle is then calculated and the ball is fire. The problem I am having is the angle that is generated does not appear correct. For example I am getting angles between 1 and 3 degrees when they should definatly be different for example 45 degree angle shots. Any ideas?
var ClickDown : Vector2;
var ClickUp : Vector2;
var point : Vector2;
var ballPrefab : Transform;
var multiplier : float;
function OnMousedown(){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0)) {
ClickDown = hit.point;
}
}
function OnMouseUp(){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0)) {
ClickUp = hit.point;
}
FireBall();
}
function FireBall () {
hyp = Vector2.Distance(ClickUp,ClickDown);
xtot = ClickUp.x - ClickDown.x;
angle = Mathf.Acos(xtot/hyp);
var projectile = Instantiate (ballPrefab, ClickUp, Quaternion.identity);
var shootVector = Quaternion.Euler(0, 0, angle) * Vector2.right;
projectile.rigidbody.velocity = shootVector * multiplier;
Debug.Log(angle);
}Mon, 05 Mar 2012 23:50:38 GMTOutRage_StudiosComment by OutRage_Studios on OutRage_Studios's answer
http://answers.unity.com/comments/223939/view.html
Im pretty sure this is what I would need, i'll have a mess around with it tonight and get back to you. Thanks a lotMon, 05 Mar 2012 20:25:08 GMTOutRage_StudiosComment by OutRage_Studios
http://answers.unity.com/comments/223938/view.html
Hi fafase is correct, essentially what I am trying to achieve is similar to the angry bird catapultMon, 05 Mar 2012 20:24:05 GMTOutRage_StudiosAnswer by fafase
http://answers.unity.com/answers/223845/view.html
Well,
you have your x and y coordinates from your mouse clicks you said.
Store the coordinates of your clicks and calculates the distance between them , you have found the hypotenuse (hyp) of your triangle. With
hyp = Vector2.Distance(click1,click2);
Now with
xtot =click.x - click1.x;
you get the adjacent side.
Now with
angle = Mathf.Acos(xtot/hyp);
you have your angle.
Is that what you need?Mon, 05 Mar 2012 14:11:04 GMTfafaseComment by fafase
http://answers.unity.com/comments/223833/view.html
I think he meant like in Worms or Angry Birds. The 2 points from the mouse would indicates the whole velocity vector. The distance would be giving the force applied to the projectile and the angle would be with the horizontal axis. Confirm or not so that we know how to help you.Mon, 05 Mar 2012 13:33:48 GMTfafaseComment by aldonaletto
http://answers.unity.com/comments/223830/view.html
@Jean Fabre is right: you need at least 3 points to define an angle - the center and the two others. Anyway, you will calculate the vectors center->point1 and center->point2, then get the angle between them with Vector3.Angle(vector1, vector2)Mon, 05 Mar 2012 13:24:22 GMTaldonalettoAnswer by jakovd
http://answers.unity.com/answers/223813/view.html
Are you sure you need that expressed as an angle? I would use vectors to fire a projectile. You get direction vector simply by subtracting first positions from the second position vector.
If you go with vectors, you can still get an angle from it, but as Jean Fabre mentioned, you need a reference vector (the one that you consider to be at 0 degrees).Mon, 05 Mar 2012 12:24:45 GMTjakovdComment by Jean-Fabre
http://answers.unity.com/comments/223757/view.html
Are you looking for the form on screen or in the 3d world where the two points are on the ground for example? Also, two points ( 2d or 3d) is not enough to define an angle, you need a third point or a frame of reference ( like the horyzontal of the first point, the screen center, etc). once you have defined a bit more what you are looking for, I'll happily tell you howMon, 05 Mar 2012 06:33:19 GMTJean-Fabre