- Home /

# Finding the angle between 2 clicked points

I am trying to find the angle between two clicked points, for example. I click down at one point and drag to the next. I can find the x/y coordinates for both points but how do i get the angle between them to ultimatly fire a projectile at that angle?

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 how

@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)

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.

Hi fafase is correct, essentially what I am trying to achieve is similar to the angry bird catapult

**Answer** by fafase
·
Mar 05, 2012 at 02:11 PM

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?

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 lot

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);

}

You don't need the angle to shoot:

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); }

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.

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.

**Answer** by jakovd
·
Mar 05, 2012 at 12:24 PM

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).

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Calculate the normal of a collider 1 Answer

How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers

2d - calculating velocity needed to pass by point 1 Answer

How can I draw a line at a specific angle? 1 Answer

follow an object's x-axis and have it translate smoothly into another object's z-axis rotation 1 Answer