Distance Travelled (x axis) as SCORE, 2D, Desperate!!!

Hello, first of all, thank you for clicking on this.

-I have a 2D infinite runner almost ready. The last thing I need is a scoring system.

-I am desperately asking for help with this!

-I want the distance travelled on the X axis displayed on the screen as SCORE and the HIGHSCORE displayed somewhere also.

I am 3 days new to scripting.

-All I can do is add your name to the credits.

Thank you a TON!

Bump… (sorry)

 score = ((startPosition.x - currentPosition.x).sqrMagnitude) * whatEverMultiplyer;

sqrMagnitude –sqrMagnitude returns the squared length ( much bigger value but without a square root operation… better performance if you run it a lot)

magnitude –magnitude returns the length of a vector (in this case how far you traveled)

or just use Distance

What you said doesn’t help me, I’m in the baby stage right now. I can semi understand a piece of code but I can’t be creative with it…

If you’re just comparing horizontal distance, you don’t even need magnitude, just subtract one from the other.
The transform.position of the player avatar gets you your current position, in world space.
Do that at the start of the level and save it.
Now at any given time, you can subtract the starting position.x from the current position.x and know how far you’ve moved (horizontally).

I’m bursting with frustration because i dont know how to make this happen. Is anyone willing to give the script that I have to attach to the player?

public class PlayerScore : MonoBehaviour {

	public int score {
		get;
		private set;
	}

	public int bestScore {
		get;
		private set;
	}
	
	private Vector3 startPosition;

	void Awake() {

		startPosition = transform.position;
		bestScore = 0;
	}

	void Update() {

		score = Mathf.RoundToInt(Mathf.Abs(transform.position.x - startPosition.x));
		bestScore = Mathf.Max(score, bestScore);
	}

	public void OnGameOver() {

		transform.position = startPosition;
	}
}

thank you, but how do I make it show on the screen?

add this method in that class:

void OnGUI() {

GUILayout.Label("Score: " + score.ToString());
GUILayout.Label("Best Score: " + bestScore.ToString());
}

thank you very much! tell me your name for the credits or maybe something you want to advertise

no need. Freebie