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 SirBoboHobo · Mar 13, 2015 at 04:28 PM · distancescore

distance based score system

I have an infinity platformer and I'd like to count distance traveled, got this code: using UnityEngine; using System.Collections;

 public class DistanceSystem : MonoBehaviour {
 
     public float distance;
     public Transform player;
 
 
     void Awake () {
         distance = Vector3.Distance (player.position, transform.position);
 
     }
     
     // Update is called once per frame
     void Update () {
         score ();
 
     }
     void score(){
         distance = Vector3.Distance (player.position, transform.position);
         distance.ToString ();
 
     }
 }


how do i display this on a text field? and when i print the value of distance it shows 500 at start, i want the starting point to be 0.

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
2
Best Answer

Answer by Rtyper · Mar 13, 2015 at 04:42 PM

The position of the object you have this on is 500 units away from your player object at the start. Make sure they start in the same location and distance will be 0 at the start. Alternatively, you could store the starting distance in a variable, and take that away from Vector3.Distance (player.position, transform.position) when setting the distance variable.

Displaying it depends on how you want to do the HUD/GUI for your game. The best option would probably be the new UI system if you're using 4.6 / 5, or the OnGUI system pre 4.6. Either way, there are plenty of tutorials out there for both. Have a look at these:

http://unity3d.com/learn/tutorials/modules/beginner/ui

http://docs.unity3d.com/Manual/GUIScriptingGuide.html

Comment
Add comment · Show 3 · 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 SirBoboHobo · Mar 13, 2015 at 06:21 PM 0
Share

I am using the new UI, could you show me how it's dont there? I've watched all videos about it, but still struggling a little

avatar image Rtyper · Mar 13, 2015 at 06:46 PM 0
Share

You would add a Label object into your scene and position it where you want on the screen to use as your score readout.

Then in your script add using UnityEngine.UI to the top, and add a Label variable that you'd reference and set from the script, using something like this:

      public Text label;
 
      void score(){
          distance = Vector3.Distance (player.position, transform.position);
  
          label.text = distance.ToString ();
      }
avatar image SirBoboHobo · Mar 13, 2015 at 08:19 PM 0
Share

Worked! thanks a lot :)

avatar image
2

Answer by maccabbe · Mar 13, 2015 at 04:47 PM

One easiest way to set the starting distance to be 0 is to initialize the position of the distance system gameObject to the player's starting position. You can also use a Vector3 variable to store the player's starting position.

There are also few ways to display the information as a text field.

1) Use the GUI class

 public class DistanceSystem : MonoBehaviour {
     public float distance;
     public Transform player;
 
     void Start() {
         transform.position=player.position;
         distance=0;
     }
     void Update() {
         distance=Vector3.Distance(player.position, transform.position);
     }
     void OnGUI() {
         GUI.Label(new Rect(10, 10, 100, 20), distance.ToString());
     }
 }

http://docs.unity3d.com/ScriptReference/GUI.html

2) Use the New UI. This is mostly done in the inspector.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui

3) Use a Text Mesh (Mesh->Text Mesh)

http://docs.unity3d.com/ScriptReference/TextMesh-text.html

http://docs.unity3d.com/Manual/class-TextMesh.html

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 gs-omeone · Jan 21, 2017 at 06:46 AM

I upvote your reply @maccabbe it's simple and really good with the various solutions but just for the gui, not bad to note the color option like for exemple GUI.color = Color.black;

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

22 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

Related Questions

Keep score after restart & limit ads/hour 1 Answer

Calculating player distance meter? C# Unity2D 1 Answer

Distance and Best Distance 1 Answer

iOS Lag From Updating Score 0 Answers

How to make the number of points equal to the distance traveled? 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