- Home /
The question is answered, right answer was accepted
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;
}
}
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
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
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
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