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 AwesomeFaceHD · Apr 01, 2014 at 07:40 PM · c#mathanglegraphcalculation

Find angle between two gameobjects?

I know this has been asked a lot, but the only answer I see is Vector3.Angle, I don't want to use that. I'm making a graphing calculator, I have everything working fine, except I can't find what angle the graphed line is. I'm using this function to try to calculate it:

 void CalculateAngle(GameObject x, GameObject y) {
     float xDis = y.transform.position.x - x.transform.position.x;
     float yDis = y.transform.position.y - x.transform.position.y;
         
     Debug.Log(xDis + " : " + yDis);
 }

The gameobects that is calls for are the first two plotted points of a line(my calculator only supports straight lines for now), I want to find the angle between those them, then spawn a long, thin cube at their position at that angle to draw the line. I thought I was onto something with that code, but I have no idea what it was. I really need some help with this, I'm not too good with math and this is hurting my brain.

Also, incase it matters, I'm not using the z axis.

Comment
Add comment · Show 5
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 McAden · Apr 01, 2014 at 07:48 PM 1
Share

Do you actually have to do the math or are you just wanting to display? If just for display you could simply use a LineRenderer drawn from the first position to the second.

avatar image AwesomeFaceHD · Apr 01, 2014 at 07:49 PM 0
Share

If anybody wants the full code: http://pastebin.com/9raT38d6

avatar image Lo0NuhtiK · Apr 01, 2014 at 07:53 PM 0
Share

http://answers.unity3d.com/questions/119313/calculating-the-angle-between-two-vectors.html

avatar image AwesomeFaceHD · Apr 01, 2014 at 07:55 PM 0
Share

@$$anonymous$$cAden I'm just wanting a line connecting the points on the graph.

avatar image POLYGAMe · Apr 01, 2014 at 08:02 PM 1
Share

You could use Unity's line renderer?

http://docs.unity3d.com/Documentation/Components/class-LineRenderer.html

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Apr 01, 2014 at 08:00 PM

To begin with, as @McAden says, you can just draw a line using the LineRenderer class. More information on drawing lines here:

http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/comment-page-1/

Plus if you are willing to spend some $, Vectrosity makes your task of drawing a line for your calculator trivial.

If you really want the angle. You can use Mathf.Atan2(). I really suggest a better name for your two game objects, but you can calculate the angle like:

 Vector3 dir = y.position - x.position;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

As for placing a quad between two points, you can do it this way:

 var dir = x.position - y.position;
 var mid = (dir) / 2.0 + x.position;
 quad.position = mid;
 quad.rotation = Quaternion.FromToRotation(Vector3.right, dir);
 quad.localScale.x = dir.magnitude * factor;

Where 'quad' is the transform of a Quad game object you are placing and rotating to match the two points. 'factor' will be 1.0 for a Quad. If you were placing some other object (cylinders work well with spheres at the joints), you'll need to adjust 'factor'.

Comment
Add comment · Show 1 · 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 AwesomeFaceHD · Apr 01, 2014 at 08:25 PM 0
Share

Thanks, I'll try this when I get a chance.

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

23 People are following this question.

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

Related Questions

Angle calculation not working?? 2 Answers

Problem with calculating damage, value returning 0 1 Answer

Problem with Clamping Values for Object's Rotation 1 Answer

Distribute terrain in zones 3 Answers

Math Genius NEEDED! Algorithm problem with parabola 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