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 /
  • Help Room /
avatar image
0
Question by Jamiemon · Sep 27, 2017 at 11:48 PM · vector2syntaxdistance check

Vector2.Distance syntax {java}

I'm trying to use Vector2.Distance to find the distance between two gameObjects in 2d, but I can't figure out the syntax & none of the answers I've found give me an explanation of how to reference the gameObjects in question. Let's call the two objects "gameCube" & "gameSphere".

 var distance = 0.0000000;
 
 
 function Update () {
 
     distance = Vector2.Distance(gameCube.transform.position, gameSphere.transform.position);

}

This didn't work, so I tried listing the gameObjects as strings:

 distance = Vector2.Distance("gameCube".transform.position, "gameSphere".transform.position);

It just tells me 'transform' is not a member of 'string'. Finally I tried listing the game gameObjects as variables:

 var gameCube = GameObject;
 var gameSphere = GameObject;
 
 var distance = 0.0000000;
 
 
 function Update () {
 
     distance = Vector2.Distance(gameCube.transform.position, gameSphere.transform.position);
 
 }


& that just tells me 'transform' is not a member of 'system.type' Am I missing something? Any help would be much appreciated ;-;

Comment
Add comment · Show 3
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 MaxGuernseyIII · Sep 28, 2017 at 12:01 AM 0
Share

This doesn't really have anything to do with Distance. this is about how to get two game objects into a behavior. Also, this is not a Java question. It's a JavaScript question. Right?

avatar image Jamiemon MaxGuernseyIII · Sep 28, 2017 at 12:07 AM 0
Share

It is javascript, & I don't know what you mean by behavior ^^; I'm trying to get it to give me a number that gets bigger the farther away the two objects are away from each other, & smaller the closer they are

avatar image MaxGuernseyIII Jamiemon · Sep 28, 2017 at 12:24 AM 0
Share

Your title says "{java}", which is misleading but only slightly so as I'm not aware of any way to use Java in Unity. Behavior meaning implementation of $$anonymous$$onoBehaviour, which is what you use to script game objects. The fact is just hidden from you in JavaScript scripts.

2 Replies

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

Answer by Kishotta · Sep 28, 2017 at 12:07 AM

In the third block, you are not correctly declaring your GameObjects. The lines you have written mean:

 var gameCube = GameObject; // gameCube is now equal to the GameObject TYPE
 var gameSphere = GameObject; // gameSphere is now also equal to the GameObject TYPE

Note that neither of these ARE GameObjects. They are the effectively nicknames for the TYPE GameObject.

The first error is telling you that the GameObject type itself does not have a .transform component (but objects of type GameObject do).

In your second block, you are trying to find the .transform of a string, which also doesn't exist.

You should use:

 var gameCube : GameObject; // gameCube is now an object OF type GameObject

Then your first attempt to find the distance will work.

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 Jamiemon · Sep 28, 2017 at 12:23 AM 0
Share

Cool All I had to change was = to : & the whole thing worked the way I thought it's supposed to Thank you ^-^

avatar image
1

Answer by MaxGuernseyIII · Sep 28, 2017 at 12:04 AM

First of all, make sure you keep the windows rolled up and the doors locked when you're writing JavaScript.

Here's an example of computing the 2D distance between two things and putting the result into a text field.

 #pragma strict
 
 var t1: Transform;
 var t2: Transform;
 var output: UnityEngine.UI.Text;
 
 function Update() {
     output.text = '' + UnityEngine.Vector2.Distance(t1.position, t2.position);
 }
 

Drag the two things you want to measure the distance between into the editor boxes in the inspector for the appropriate instance of the script as well as the label to hold the result.

alt text


distancescriptinjavascript.png (23.7 kB)
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

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

Where can I get this MonoDevelop syntax highlighting theme? 0 Answers

Best way to spawn objects randomly on only x-axis on Mobile 0 Answers

Vector2 Point Density Changing 0 Answers

Measure apparent surface from arbitrary angle 0 Answers

Enemies shoot slower if the player is far away 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