Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
8
Question by ChrisJoosten · Apr 30, 2014 at 12:01 PM · c#raycastingdirectiondistanceangle

How to calculate direction between 2 objects

Hi guys, I'm trying to figure out how to calculate the direction between 2 objects. I already got this.

         Debug.DrawLine (camera.transform.position, this.transform.position, Color.red, Mathf.Infinity);


With this code i get a DrawLine between object 1 (camera) and object 2 (my object) But i need the line to go further then just object 2, so I need to know the direction/angle or something.

So my question is: How to calculate the direction/angle or something between these objects.

Comment
Add comment · Show 1
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 Nerevar · Apr 30, 2014 at 12:11 PM 0
Share

Hi,

You need 3 points/objects to calculate an angle or 2 vectors. With this code you don't have enough information to get an angle, you need a referential item...

I hope it is clear :p

regards

2 Replies

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

Answer by Berenger · Apr 30, 2014 at 12:14 PM

Let's say camera is your point A, and this is your point B.

Vector3 AB = B - A. Destination - Origin.

This is a direction and a distance. To have only the direction (and a distance of 1), you have to normalize it.

AB = AB.normalized OR AB.Normalize()

In your case :

 Vector3 pos = camera.transform.position
 Vector3 dir = (this.transform.position - camera.transform.position).normalized
 Debug.DrawLine (pos, pos + dir * 10, Color.red, Mathf.Infinity);
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 ChrisJoosten · Apr 30, 2014 at 12:22 PM 0
Share

Thank you, this was exactly what I needed :)

avatar image
3

Answer by HarshadK · Apr 30, 2014 at 12:11 PM

Check this out: Direction and Distance from One Object to Another

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 SuperVidde · Mar 30 at 11:10 AM 1
Share

Sorry... that page seems to be missing!

avatar image Chuckatronic SuperVidde · May 27 at 01:38 PM 0
Share

It's about half way down this page

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

28 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

Related Questions

Distribute terrain in zones 3 Answers

Raycasting with Rotation of Camera 0 Answers

how to get a correct rotation 2 Answers

8 Directional sprite in 3d world, how to retrieve direction? 2 Answers

Getting the distance in a known direction 2 Answers


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