#Calculate time to destination.

1 messages · Page 1 of 1 (latest)

civic delta
#

Hey, I need to calculate the time it takes for my player to reach a destination so I can adjust the animation speed.
so I thought I could just use Time = Distance / Speed, but this doesn't seem to be working.
When I subtract the starting time from the current time I get a different outcome that seems to also be the correct time it takes to reach the destination.

At first I thought Time.deltaTime could be the cause but I found a forum post that says what I am doing should work but that post might be wrong.
The forum post in question.

minClimbDistance is set to 0.01f and is not the issue, I tried changing it to this.transform.position != climbDestination but the issue remained.

private IEnumerator LedgeClimb()
{
    float startTime = Time.time;

    currentClimbDistance = Vector3.Distance(this.transform.position, climbDestination);

    headAnimator.speed = currentClimbDistance / climbSpeed + 1;

    print(currentClimbDistance / climbSpeed);

    while (currentClimbDistance > minClimbDistance)
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, climbDestination, climbSpeed * Time.deltaTime);
        currentClimbDistance = Vector3.Distance(this.transform.position, climbDestination);

        yield return null;
    }
    
    print("Climb Time: " + (Time.time - startTime));
}

Any help with this would be much appreciated, thank you for reading!

zealous crane
#

headAnimator.speed = currentClimbDistance / climbSpeed + 1;
this is wrong btw. this should be climbSpeed / currentClimbDistance (aka 1/time)

#

are they perhaps both ints

#

i just thought of it when you sent that

#

wait no it's from Distance, that can't be it..

civic delta
#

that does seem to fix it so that's awesome but I still don't get why it gives me a totally different number when I print climbSpeed / currentClimbDistance
climbSpeed / currentClimbDistance gives me 2.185821 but Time.time - startTime gives me 0.57724

zealous crane
#

those sound like approximate inverses

#

the time should be currentClimbDistance / climbSpeed

civic delta
#

either way it works now so thank you!

zealous crane
#

that's a lotta lag

#

that might just account for the discrepancy

civic delta
#

Oh no I slowed the video waaay down

#

I think that's on 0.01x speed lol