0

I currently learn unity3d and start to create an endless game, and I want to add score by a distance that player has traveled on + x axis, so I want to calculate only how far the player traveled on + x axis.. how to calculate it?

this is my scripts

public class Score : MonoBehaviour
public float distance;
public Transform player;
private float score = 0f;
public Text scoreText;

void Awake()
{
    distance = Vector3.Distance(player.position, transform.position);
}


void Update()
{
    if (distance > player.transform.position.x)
    {
        score = distance ++;
        scoreText.text = distance.ToString();
    }
}

}

2 Answers2

0

You can have something like this to get you started. I set it to 0.1 distance to get +1 score but that number can be anything of course.

private const float DISTANCE_REQUIRED_FOR_1_POINT = 0.1f;
private Vector3 previousPlayerPosition;
private float distanceSinceLastScore;

public Transform player;
private float score = 0f;
public Text scoreText;

void Awake()
{
    //At the start save the start position.
    previousPlayerPosition = player.position;
}


void Update()
{
    //Calculate the distance travvelled since last update.
    distanceSinceLastScore += player.position.x - previousPlayerPosition.x;
    //Set the previous position the current.
    previousPlayerPosition = player.position;

    int scoreToAdd = 0;
    while (distanceSinceLastScore > DISTANCE_REQUIRED_FOR_1_POINT)
    {
        //Calculate how many points to add in this update. (Likely always 1 or none)
        ++scoreToAdd;
        distanceSinceLastScore -= DISTANCE_REQUIRED_FOR_1_POINT;
    }

    if (scoreToAdd > 0)
    {
        score += scoreToAdd;//Add to the total score.           
        scoreText.text = score.ToString();//refresh the text display.
    }
}
leo Quint
  • 644
  • 1
  • 5
  • 11
0

To be honest, you gave very little information about the game. But I will try to come up with some possible scenarios and solutions.

First of all, you stated that this is an endless game, then I don't know what distance are you calculating between the player and a point. So, your code didn't make much sense to me, please explain if I'm missing something.

Now, you said you want to calculate the score based on the distance that the player has traveled on + x-axis. So, take a look at this code:

float previousPlayerX;
public Transform player;
private float score;

void Awake()
{
    previousPlayerX= player.position.x;
    score = 0;
}

void Update()
{
    if (previousPlayerX < player.position.x)
    {
        score += player.position.x - previousPlayerX;
        previousPlayerX = player.transform.position.x
    }
}

This code will increase the score as long as the player is running to +x. If the player stops or even turns back, the score will not be updated. Also, note that if your starting position is 0, then you don't need to subtract the two positions to add the score, you can simply go ahead and say score = player.position.x and it should give you the same result. Furthermore, I would implement this code in a coroutine with a little bit of wait time to avoid possible bugs maybe.

Now, let's think about another scenario. I don't think the character doesn't go to infinity in endless run games, generally, the character object is stable and you kinda make that illusion of endless flow. So, let's say your character's position is not changing actually, but in the game, it seems like your character is running non-stop. In such a case, you may want to increase the score based on the time passing.

private float score;

void Awake()
{
    score = 0;
    StartCoroutine(UpdateScore());
}

IEnumerator UpdateScore()
{
    while(true)
    {
       score += 1;
       yield return new WaitForSeconds(1);
    }
}

As you can see in the second code, I'm just assuming that my character runs non-stop and I'm just increasing the score by 1 every second. You can do it in every half a second, you can increase the score by multiplying with a speed value or you can put an if statement and don't increase the score if the character is stunned for example. So, it's all up to you.

I hope that answer helps and gives you a general idea. Let me know in the comments if you have any other questions related to this.

Onurcan Onder
  • 386
  • 1
  • 6
  • I am sorry because I didn't gave more full information about my problem, I want to calculate the distance from the start point to the last position of my player. when the player died in a distance. the last checkpoint is the score.. thank you for your answer – aldo muhammad May 10 '20 at 23:04