If you are raycasting at a collider you are walking on, the RaycastHit object filled out (or returned) by the Raycast call contains a normal vector. You can determine the local slope by dividing the magnitude of the X and Z components of the normal by the magnitude of the Y component and decide what you want to do, something like:
Vector3 norm = hit.normal; // hit is the RaycastHit object
float slope = new Vector2( norm.x, norm.z).magnitude / Mathf.Abs( norm.y);
I am still not sure for an easier way. I am at work so i cant test it BUT:
slope in % / distance = Heightmeter?
for example
12 / 100 = 0,12 (0,12 = 12 Height meter driven on 100 Meter distance.
Vector3 startPosition; // Set this at the start of the level
Vector3 currentPosition; // Populate this however you are tracking your current position
float totalClimb = currentPosition.y - startPosition.y;
This only tracks the net climb, though. If you want the climb without subtracting any falls, you’ll have to accumulate positive y changes over time.
also a good idea but when i have hills? i have up and downs, i need always the ups
100meter up then 100 meter down then again 100meter up, then i have climbed 200meter.
What i would consider to do however is keeping track of all the position of the player in an array (per example), in that way you will be able to do a lot of post calculation and display statistics/graphs to the player after the race.
But it depends of the race lenght as memory consumption can become a problem. You could choose to not keep the player position of every frame in memory though
Switch totalClimb += currentYPosition.y - startPosition.y; to totalClimb += currentYPosition.y - lastYPosition.y;. Also, you won’t need startPosition with this method.