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 /
avatar image
1
Question by AlejandroBoss10 · Oct 02, 2017 at 09:57 PM · scripting problemupdatedistance check

How to make a distance counter that updates?

So I have the basic script done in JavaScript, going to convert it to C# soon. What my script does as of right now is that it says how far two objects are from each when the game starts. What I want is that the distance updates as the object with the script moves closer or farther to the target. It displays how far something is at the beginning in the console, but it does not update as the player moves closer or farther from the object. This is the script

var other : Transform; if (other) { var dist = Vector3.Distance(other.position, transform.position); print ("Distance to other: " + dist); }

Any help would be greatly appreciated. Thank you and if anyone needs this script, feel free to use it.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AlejandroBoss10 · Oct 02, 2017 at 10:17 PM

Ok, so the fix was very very simple. Instead of JavaScript, I turned it into C#. THIS SCRIPT IS IN C# NOW. Instead of having the code in the Start function, I changed it to the Update function. Now whenever the player moves around, either closer or farther from the object, it shows how far it is from the other object in the console. I will make it so that the player can see there distance in the game soon. This is the script

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ScriptMeter : MonoBehaviour { public Transform other;

 private void Update()
 {
     float dist = Vector3.Distance(other.position, transform.position);
     Debug.Log("Distance to other: " + dist);
 }

}

feel free to use this.

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
avatar image
0

Answer by Topthink · Oct 02, 2017 at 10:18 PM

Consider this:

 var other : Transform;
     if (other) {
         var dist = Vector3.Distance(other.position, transform.position);
         print ("Distance to other: " + dist);
     }

Of course, instead of "print" you put your debug there. You can have anything there at "other" or change the "if" statement, etc.

Good Luck.

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 AlejandroBoss10 · Oct 06, 2017 at 02:40 PM 0
Share

I feel kind of dumb asking this, but is this in C# or JS?

avatar image Tom01098 AlejandroBoss10 · Oct 06, 2017 at 02:46 PM 0
Share

It's in JavaScript (UnityScript really). If you are still using UnityScript, please consider switching to C# as support is being dropped soon : https://blogs.unity3d.com/2017/08/11/unityscripts-long-ride-off-into-the-sunset/

avatar image
0

Answer by MaxGuernseyIII · Oct 02, 2017 at 11:31 PM

I think what you want is to know how to update a counter, like a visual thing that people see on the screen of the game. I would do this using a Text component.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class UpdateDistance : MonoBehaviour
 {
   public Transform Object1;
   public Transform Object2;
   public Text Distance;
 
   void Update()
   {
     Distance.text = Vector3.Distance(Object1.position, Object2.position).ToString();
   }
 }
 
Comment
Add comment · Show 4 · 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 AlejandroBoss10 · Oct 06, 2017 at 02:41 PM 0
Share

That's what I meant @$$anonymous$$axGuernseyIII . Thank you, I will try this out and see what happens. Thank you

avatar image AlejandroBoss10 · Oct 06, 2017 at 03:21 PM 0
Share

I love this, it works just as you said. It it also very plain and simple. One thing that I would like to know is, how would you make it so that ins$$anonymous$$d of showing a float, it should show a whole number so an int. Thank you so much for this plain and simple script.

avatar image MaxGuernseyIII AlejandroBoss10 · Oct 06, 2017 at 09:12 PM 0
Share

I left the formatting to you. You can pass a format string into Single.ToString(). There are a lot of options, including some standard format strings and custom format strings. The format you want is probably just "D", which is the standard specifier for the integer digits. An alternative would be a custom string like "###,##0", which means "the x10^0 digit unconditionally, plus up to five more significant digits with an optional thousands separator".

avatar image MaxGuernseyIII MaxGuernseyIII · Oct 06, 2017 at 09:14 PM 0
Share

Actually, "," is more coupled to the concept of thousands being what gets separation than I thought. You can could also try something like "#,#". Anyway, format strings are a wonderland of options and I wouldn't presume to tell you which one is the right one for you.

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

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

Script and Start function of script are running, but Update function is not? 0 Answers

Can you run a coroutine more often than Update? 1 Answer

inconsistent results calculating the distance moved by an object 1 Answer

How to trigger udpate in editor mode only when I modify component parameters 1 Answer

Extracting Values from Update Function 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