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 Jogabba · Apr 06, 2014 at 09:22 PM · vector3directionobjectsbetween

Vector between two objects

This is a very basic question. I want to find the direction vector between two objects. Seems very easy, so I can't find out why my code does not work.

 var obj2: GameObject;
 var direction = obj2.transform.position - transform.position;
 
 function Start(){
 
 Debug.Log(obj2.transform.position);
 Debug.Log(transform.position);
 Debug.Log(direction);
 }

The Debug.Log(direction) always returns 0,0,0. Also,the console says that "Object reference not set to instance of an object". I don't really know what this means. I made sure that there was an object assigned to the obj2 variable.

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 tarasfromlviv · Apr 06, 2014 at 10:02 PM 0
Share

The problem is that you are trying to compute the direction as a field. When you are using obj2 variable to compute the direction it is not initialized yet (There is no way for script to know that you did set it in unity inspector). So just do you direction calculation inside the Start() or Awake() method.

If you use C# to write the same code it will actually tell you the same problem A field initializer cannot reference the nonstatic field, method, or property TestBehaviourScript.obj2 which actually means that you cannot initialize field in this way using another's field value.

1 Reply

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

Answer by Radivarig · Apr 07, 2014 at 12:39 AM

If obj2 is not assigned, you will get the result zero, because the operation of assigning the right side of expression will return null exception and the left value will then be set to default. Usually that is prevented with if (obj2 != null).

The error will also happen even if you assign the object but call it before start. You should call the calculation from Start instead and only initialize your variable before.

If you want to get the vector between two points (or two object positions) you should add them, not subtract. Here is a working modification of your code:

 #pragma strict
 var obj2: GameObject;
 var direction: Vector3;
 
 function Start(){
     direction = obj2.transform.position + transform.position;
 
     Debug.Log(obj2.transform.position);
     Debug.Log(transform.position);
     Debug.Log(direction);
 }

And here is my version of your code, you can play with it in scene view while in play mode like here, just drag the GameObject around.

alt text

 #pragma strict
 var obj2: GameObject;
 private var direction;
 
 function Update(){
     DoStuff();
     DebugOurStuff();
 }
 
 function DoStuff(){            
     if(obj2 != null)
         direction = CalculateMidVector(obj2.transform.position, this.transform.position);
 }
 
 function CalculateMidVector(first : Vector3, second : Vector3){
     return first + second;
 }
 
 function DebugOurStuff()
 {
     Debug.DrawLine(Vector3.zero, obj2.transform.position, Color.blue);
     Debug.DrawLine(Vector3.zero, this.transform.position, Color.cyan);
     Debug.DrawLine(Vector3.zero, direction, Color.green);
 }



midvector.png (44.2 kB)
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 PlanetLotus · Mar 10, 2016 at 05:40 AM 1
Share

Old answer but I wanted to chime in in case anyone else's vector math is as rusty as $$anonymous$$e is. I think subtraction is what's wanted here, at least it was for my scenario. I subtract vector1 from vector2, then normalize the result, to get a directional vector from vector1 to vector2. Does that make sense?

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

24 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

Related Questions

Adding velocity to an object based on angle between it and another? 2 Answers

What is a way to use a value and return it back to zero? 1 Answer

How to moving objects up and down slowly? 2 Answers

How can I move character controller in one direction 0 Answers

teleport player in the direction they are looking, 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