Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheNinjassin · Feb 04, 2016 at 08:50 AM · rotationvector3vector

Find rotation between up and two vectors

Hi guys,

this one's probably really simple and I'm just being dense but I need to find the gradient of a line between vector a (Xa, Ya) and vector b (Xb, Yb). I need to instantiate a game object with the rotation which is consistent with the line that runs from point A to B. I need to find the gradient as degrees so I can apply the rotation to the Z axis of my GameObject so as 0 is vertically up, 45 is up-right, 90 is right etc.

I think I'm just being dense but any help would be appreciated.

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cjdev · Feb 04, 2016 at 09:05 AM

I'm not entirely sure if I've got you right but if you are looking to find the angle that the line segment makes on a 2D plane with an axis you can make a triangle out of the change in X coordinates for a side, and the Y, and then use the tangent of the opposite side over the adjacent to find the angle.

Comment
Add comment · Show 2 · 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 TheNinjassin · Feb 04, 2016 at 09:13 AM 0
Share

That's what I was doing but It was only working for me between 0 and 90 degrees.

public Vector3 findRotation(Vector3 a, Vector3 b) { float x, y, rot; x = a.x - b.x; x = $$anonymous$$athf.Sqrt(x * x); y = a.y - b.y; y = $$anonymous$$athf.Sqrt(y * y); rot = $$anonymous$$athf.Rad2Deg * $$anonymous$$athf.Atan (x / y); Vector3 rotation = new Vector3 (0, 0, rot); return rotation; }

obviously when I get beyond 90 degrees that becomes an issue and it just calculates it as if it were between 1-90 again.

avatar image TheNinjassin · Feb 04, 2016 at 09:20 AM 0
Share

nvm I don't know why I was making the x and y values positive :') so dense.

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

43 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 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

Clamping Vector3 2 Answers

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer

How to make Y-Axis face away from a position? 2 Answers

Rotating a direction Vector3 by Quaternion 2 Answers

Player not facing the mouse correctly 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