# [SOLVED]How to count/measure a climb?

I want to count only the positive distance of “y” Position

I know that i can count distance with:

``````distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
``````

is there also a option for a climb?
something like : when slope >= 0.01 then count climb units…

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 do no raycast, there must be an easier way i hope.

It’s hard to imagine something simpler than using Physics.Raycast to determine the slope of ground below you, but good luck to you!

I dont understand how your sample give the distance, if you see my game here:

i have the percent of slope, but i dont understand how measure the meters of climb only

If the Y+ axis is straight up, each frame before you move note what the Y position is, move, then note the new Y position.

Subtract those two values and that is how much you climbed this frame.

Sum them up and that is how much you climbed total.

1 Like

That was the information i needed Thank you

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.

You could also use this idea:

``````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.

1 Like

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.

Then you should also keep track of the last position.

Instead of checking if the new position is higher than the start position, check if it’s higher than the last position of the player.

``````Vector3 lastPosition;
Vector3 currentPosition;
float totalClimb=0;

void update(){

currentPosition = transform.position;

if(currentPosition.y > lastPosition.y)
totalClimb += currentPosition.y - startPosition.y;

lastPosition = currentPosition;
}
``````

Something like this

1 Like

Great solution!

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

1 Like

hmm it is not working how i except it. the total climb value counts up but there is no y change:

`````` public float distanceTravelled = 0;  //for distance

Vector3 lastYPosition;
Vector3 currentYPosition;
public Vector3 startPosition;

public float totalClimb = 0;
// public float distanceClimbed = 0; //for climb

Vector3 lastPosition;

public float weight;

public void Start()
{
lastPosition = transform.position;
lastYPosition = transform.position;

ProfileSessionManager.GetUserWeight();
weight = ProfileSessionManager.curPlayerWeight;
}

public void Update()
{
//How far we ride
distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;

//How many heightMeter we have climbed
currentYPosition = transform.position;

if (currentYPosition.y > lastYPosition.y)
totalClimb += currentYPosition.y - startPosition.y;

lastYPosition = currentYPosition;

}
``````

Where is my fail?

Switch `totalClimb += currentYPosition.y - startPosition.y;` to `totalClimb += currentYPosition.y - lastYPosition.y;`. Also, you won’t need startPosition with this method.

1 Like

It works!
Thank you ALL for your help, if you re in my Region i spend a beer or two for all!