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 /
This question was closed Aug 15, 2018 at 06:43 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · May 13, 2018 at 01:12 PM · distancescoreequal

How to make the number of points equal to the distance traveled?

Is it possible to assign the number of points to the height?

My object moves up (Y axis), for which I get points.

The problem is that the number of points is not equal to the distance, for example: for 19 units I gets 436 points (look photo).

While I would like the number of points to be equal to the distance traveled.

How to make the number of points equal to the distance traveled?

Could someone help you?

I'm using this script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : MonoBehaviour {
 
     public Text scoreText;
 
     [SerializeField]
     int score = 0;
 
     [SerializeField]
     Vector3 _lastPosition;
 
     // Use this for initialization
     void Start () {
         _lastPosition = this.transform.position;
     }
 
     // Update is called once per frame
     void Update () {
         if( this.transform.position.y > _lastPosition.y  )
             score++;
         else{
             score--;
         }
 
         _lastPosition = this.transform.position;
     
         scoreText.text = "Score" + score;
 
             }
 }
 

alt text

score.png (7.0 kB)
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by troopy28 · May 13, 2018 at 01:30 PM

Hello,

Your Update function is called once per frame. If your object has a big enough speed, there won't be a call to Update for each integer coordinate "travelled". Your script will detects regularly that you have travelled, but won't know how far. If you want to add a point per coordinate, I suggest you do the following modification in your script :

 score += this.transform.position.y - _lastPosition.y; 
 // Also, use a float score, or cast this substraction to an int

Instead of :

 if( this.transform.position.y > _lastPosition.y  )
     score++;
 else {
     score--;
 }

This way, you don't have to do the if check, because the result of the substraction will be positive when going upward, and negative when going down, thus decreasing your score.

Regards, troopy28

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 polan31 · May 13, 2018 at 03:22 PM 0
Share

Hey, thanks for the answer. Could you write yet how it looked with " int"?

I'm a beginner, so I do not get it all.

But, after changing to float and adding:

 scoreText.text = "Score" + $$anonymous$$athf.Round (score);


everything works beautifully, but I would like to know on the future what it would look like with an "int".

Thanks for best answer

avatar image troopy28 polan31 · May 14, 2018 at 04:21 PM 0
Share

You could apply your round directly to the substraction, like this:

 score += $$anonymous$$athf.Round(this.transform.position.y - _lastPosition.y); 

But I don't think it is the way to go. You're here dealing with decimal coordinates, so I think it is better to treat it as they are: decimal coordinates. What you display to the user is not what truly matters. $$anonymous$$oreover, rounding the score everytime will result in undefined results: if your speed is not big enough, the round will always be 0 (if you travelled 0.198 between two Update calls for instance), and the score will never get updated.

Follow this Question

Answers Answers and Comments

83 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

Related Questions

Distance and Best Distance 1 Answer

distance based score system 3 Answers

Keep score after restart & limit ads/hour 1 Answer

iOS Lag From Updating Score 0 Answers

Calculating player distance meter? C# Unity2D 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