Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by Gian Hancock · Nov 02, 2010 at 08:20 AM · positionvector3directionanglepoint

how to find direction between two points

hey i need to know how to find the angle between two different points. For example if i had two different points pointA and pointB how would i find the global rotation required for pointA to face pointB. 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

2 Replies

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

Answer by StephanK · Nov 02, 2010 at 09:31 AM

A point doesn't have an orientation, so a point can't "face" another point. If you want to make your GameObject face a certain point you can use transform.LookAt() to get the angle between two Vectors (not points) you can use Vector3.Angle()

EDIT: To get a ray from point A in the direction of point B you can use point A as origin and (pointB - pointA).normalized as the direction.

Comment
Add comment · Show 9 · 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 Gian Hancock · Nov 03, 2010 at 06:18 AM 0
Share

Thanks for the responses and sorry if i was vague with the information. $$anonymous$$ore specifically i wanted to create a ray that points from its origin to another object and i wanted to know how to go about getting the direction for the ray that is required when constructing the ray.

avatar image StephanK · Nov 03, 2010 at 08:21 AM 0
Share

I edited my original answer.

avatar image Gian Hancock · Nov 03, 2010 at 10:23 AM 0
Share

Thanks for the help everything works fine now, although i don't understand mathematically how normalizing the difference between to vectors gives the direction, but i guess that is extra information i don't need to know anyway :p. Thanks again for the help.

avatar image StephanK · Nov 03, 2010 at 03:49 PM 0
Share

Well it maybe an extra information, but it's a useful one. Vectors have a direction and a length(magnitude). A direction is a so called unit vector, which has a length of 1. Normalizing a vector will give you its direction. $$anonymous$$athematically it divides the vector by its magnitude returnin a vector of length 1.

avatar image Gian Hancock · Nov 04, 2010 at 06:31 AM 0
Share

interesting, so if i have a vector of [1,1,1] its magnitude is is 1.7 and its normalized version is [1/1.7, 1-1.7, 1/1.7] or [.588,588,588]?. if i am correct so far, how would i go about converting this into a Euler or something more useful then just a vector?

Show more comments
avatar image
1

Answer by Proclyon · Nov 02, 2010 at 09:46 AM

Speaking in general math/physics , without to much knowledge of how unity scripting supports the "getters" of this information I may be able to give you the information that you will need. How to get that information is tricky for me at the moment and I will have to direct you "HERE": http://unity3d.com/support/documentation/ScriptReference/index.html for that but I will try giving you some potential candidates to clear up search time, just excuse my lack of testing these idea's.

First of all there are 2 objects here.

Your object A and you Object B.

You want object A to look at Object B if I interpreted your question corrrectly , if not than I should have assumed you want to copy the rotation of object B onto Object A making them both face the same direction. To do that simply get transform.rotation information stored into a variable and create a new rotation quaternion for the A object with the following code sample.

Code for assumption 2 in which you want to copy the rotation of B to A:

Quaternion q = ObjectB.transform.rotation;
transform.rotation = q; 

This may not work in C# though due to writing permissions on transforms requiring a new keyword in which you create a new (in this case) struct.

This quote from the Unity3D scripting references site explains why you don't want to go into details with Quaternions and simply want to copy paste:

Quaternion Struct

Quaternions are used to represent rotations.

They are compact, don't suffer from gimbal lock and can easily be interpolated. Unity internally uses Quaternions to represent all rotations. However, they are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations). However, they are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations).*

Excuse me if this sample will not work I haven't had the time to test it, but I hope it will be enough to get yon on your way!

If indeed you want to LOOK at the object instead of copy the rotation you would need to do this:

Assumption 1 sample:

Get the location of the target (transform of position B) Use the transform.LookAt member

function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void //JS
transform.LookAt(target);                                                   //C#

Hope this is enough information for you to deal with the problem. It's not a great answer I know. But please help yourself to quick answers by adding information such as: Target platform, Compiler, code language used or any other information that can narrow down the search scope for the answer you need.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

Transform angle to grid position 2 Answers

Why are my player's positions not being set correctly? 1 Answer

Finding difference between gameObject's x coordinate according to one object's transform 1 Answer

Find a point between 2 vectors for a specific Y 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