How to calculate distance traveled given time and speed
Hey guys, I'm trying to figure this out but so far not as good as it should be, so of course I'm doing something wrong. So in my game I'm throwing a ball based on a power bar, this power bar only affects speed but the ball always travels the same amount of time which is 2 seconds. What I'm trying to do is add a crosshair to predict how far the ball will travel according to the current power.
The balls Update method runs this code:
protected virtual void Update () {
transform.Translate(transform.up * speed * Time.smoothDeltaTime * power);
}
Power is a float that goes from 0 up to 1, max. The thing is I can't get the crosshair to actually mark the correct spot, here's my update method for the crosshair:
void Update () {
Vector3 newPosition = transform.position;
newPosition.y = (totalRange * power) + minY ;
transform.position = newPosition;
}
That class has a minY and a maxY which are calculated given the camera size, and the totalrange is the sum of minY's absolute value and maxY. The thing is, for the maximum or minimum value of power the results are correct given the size of the camera but its not accurate for the travel distance of the ball since it moves exponentially, for example, the ball barely moves with a power less than 0.15. I know that Distance = Speed * Time, Time I know, but a speed when the ball moves with translate? Anyway, I guess is something stupid that I can't get my head around of due to the lack of sleep xD
Thank you all for reading me!
Edit: So I finally got it working, in the end I was forgetting about those 2 seconds in my formula, so I ended up doing (totalRange power smoothDeltaTime TravelTime framerate) + minY All I needed was a good night sleep. Of course, since movement and calculation is based on framerate, I'll provide an area estimation which is pretty close, in all my tests the ball falls practically on the spot, sometimes with a 0.05 - 0.10 offset, therefor I'll provide some area icon so the player knows it's going to fall pretty close to the target area.
Your answer
Follow this Question
Related Questions
Move 3D object on transform.localposition 1 Answer
Moving walls in multiple places 1 Answer
Objects follow player and mimics player movement (C#) 0 Answers
How to make movement smooth? 0 Answers