- Home /
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.
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.
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.
I feel kind of dumb asking this, but is this in C# or JS?
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/
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();
}
}
That's what I meant @$$anonymous$$axGuernseyIII . Thank you, I will try this out and see what happens. Thank you
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.
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".
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
Follow this Question
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