Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OutRage_Studios · Mar 04, 2012 at 02:12 PM · 2dphysicsquaternionanglepoint

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jean-Fabre · Mar 05, 2012 at 06:33 AM 1
Share

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

avatar image aldonaletto · Mar 05, 2012 at 01:24 PM 0
Share

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

avatar image fafase · Mar 05, 2012 at 01:33 PM 1
Share

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.

avatar image OutRage_Studios · Mar 05, 2012 at 08:24 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OutRage_Studios · Mar 05, 2012 at 08:25 PM 0
Share

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

avatar image OutRage_Studios · Mar 05, 2012 at 11:50 PM 0
Share

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 On$$anonymous$$ousedown(){

var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;

if (collider.Raycast (ray, hit, 100.0)) { ClickDown = hit.point; } }

function On$$anonymous$$ouseUp(){

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 = $$anonymous$$athf.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);

}

avatar image aldonaletto · Mar 06, 2012 at 02:41 AM 2
Share

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);
}
avatar image fafase · Mar 06, 2012 at 05:49 AM 1
Share

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.

avatar image OutRage_Studios · Mar 06, 2012 at 11:30 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculate the normal of a collider 1 Answer

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

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

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges