• 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 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
32
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
2

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

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Getting the distance in a known direction 2 Answers

Multiple Cars not working 1 Answer

Weird angle returned 1 Answer

How to get the direction of the velocity of an object (c#) 1 Answer

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges