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 Dris · Jan 26, 2015 at 04:38 AM · timedistancescorescoreboardmeter

Calculating player distance meter? C# Unity2D

Hello,

this is my first post here as a newbie to Unity. I just started learning c# few days ago and created my first game. Nevertheless, I encountered some minor issue of which is, calculating the player (flying and running sprites) distance. I managed to display the meter icon and "0" but it don't know how to code it to calculate the player's run.

here is what I did:

public Text meterLabel;

void meter{

meterLabel.text = meter.ToString();

}

Note: Im new to unity c# coding, i learnt that from some post i found in search for solution to my problem.

Apologies.

Any help would be appreciated.

Thanks in advance

Comment
Add comment · Show 2
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 Dris · Jan 26, 2015 at 06:09 AM 0
Share

@$$anonymous$$akecodenow,

how do you mean? Please can you give me a brief insight? Im totally new at this.

avatar image Dris · Jan 27, 2015 at 02:40 PM 0
Share

@immersiveGamer,

Thank you for the scripts. $$anonymous$$y actual game idea is similar to jetpack joyride. How do I implement it?

Thanks again ;)

1 Reply

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

Answer by immersiveGamer · Jan 26, 2015 at 06:27 AM

There are several ways you can calculate the distance. Do know that unity units are meters. But that really doesn't mater too much for most games.

Speed

If you know the speed at which your player is going at and it is a constant speed you can do speed x time. And that would give you the distance.

 float startTime = Time.time;
 float currentTime;
 
 Void Update()
 {
      currentTime = Time.time;
      float meters = (currentTime - startTime) * speed;
      meterLable.text = meters. ToString();
 }

Distance If you know the start point you can subtract the x values to get the distance.

 Vector3 startPoint;
 
 void Awake() 
 {
     startPoint = transform.position;
 }
 
 void Update()
 {
     float meters = transfom.position.x - startPoint.x;
     meterLable.text = meters.ToString();
 }

If you need to know some more about programming in C# I suggest picking up a book at the library. Also there are some good unity tutorials out there. Learn more about unity's API and what there is that you can use.

Comment
Add comment · Show 5 · 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 meat5000 ♦ · Jan 27, 2015 at 02:39 PM 0
Share

Do know that unity units are meters

They are Units. Unspecified, is the relation to an actual measurement.

avatar image immersiveGamer · Jan 28, 2015 at 02:20 AM 0
Share

eh, yes for most basic games it doesn't matter but it really is meters because when you import models and do the units to meters it will be 1 meter to 1 unit and physics is based off of meters and kilograms http://answers.unity3d.com/questions/517442/what-are-the-real-world-measurements-in-unity.html

avatar image PerguntasUnity · Oct 30, 2016 at 06:34 PM 0
Share

Hey!

I used the example of the speed, but it appears to float, I wanted to appear in int, to do this?

avatar image Axtrainz PerguntasUnity · Oct 30, 2016 at 06:37 PM 0
Share

just parse the value: int number = (int)value;

avatar image PerguntasUnity PerguntasUnity · Oct 30, 2016 at 06:42 PM 0
Share

I found using it:

  .ToString ("0");

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Keep score after restart & limit ads/hour 1 Answer

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

Points - Kills fps 1 Answer

Calculate the distance a object has moved. 3 Answers

Score not increasing after killing 1 enemy 2 Answers


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