- Home /
How to add progress bar showing player movement from start to finish line in 3d game ?,
I have added progress bar which is working fine for straight path. but in curve path as shown in figure the progress bar starts moving backwards due to decrease in z position of car. I have added many check point to calculate the distance between them and added them. but issue with current position. how to manage current position of car and move the progress bar.
public Transform LevelEndCheckpoint;
public Slider ProgressSlider;
private float totalDistance,current_position,playerPosition;
// Use this for initialization
void OnEnable () {
playerPosition = this.transform.position.z;
//totalDistance = LevelEndCheckpoint.transform.position.z - playerPosition;
for (int i = 0; i < p_Length; i++) {
totalDistance += Vector3.Distance( AllPoints [i + 1].position , AllPoints [i].position);
}
ProgressSlider.maxValue = -totalDistance;
ProgressSlider.minValue = -playerPosition;
}
void Update () {
playerPosition = -this.transform.position.z;
ProgressSlider.value = playerPosition;
}
![alt text][1] ![alt text][2] [1]: /storage/temp/121792-screenshot-10.png [2]: /storage/temp/121793-screenshot-11.jpg
,
Answer by madks13 · Jul 30, 2018 at 09:20 AM
Umm, since your game seems to be a racing game, why not have a simple float as the remaining distance and display that as a progress bar? For circular tracks, you can calculate the total distance, then calculate the remaining by substracting the distance covered.
Edit : to be clear, i'm not talking abour the distance covered by the car, but the track distance covered. You could have a car spin around and go backwards, the distance covered by the car would easily be greater than the total track length. Have to calculate the track distance covered by the car.
But if the car goes back, what then? I think the best is to have so many checkpoints, like 100, so you can easy track the relative position in the race track.
Bye
I never said not to use checkpoints. I only said to measure the track length. For that, you CAN use checkpoints. Let's say you have 100 checkpoints. On the progress bar, you would add 1% for each checkpoint done. If the car goes back, it won't register since once a checkpoint is passed you would deactivate it, only going further would continue the count. Also, you might want to activate only the next checkpoint to go to, since in a circular track one would be able to just go backwards and finish the race instantly.
In any case, the way you calculate the length of a track and the remaining distance to do is not really important. From OP's description, it seems coordinates are used currently, and that is not track length. Hence my suggestion to switch to track length and remaining distance.
I have used 30 trigger colliders to calculate the length of track. the length is calculated accurately. problem with car position. if i move progress bar with trigger position with collider it is not moving smooth i.e the progress bar stops until we hit the collider. and for passing current z position to the progress bar there is issue in curve path. the progress bar moves backward on turning due to decrease in z position. I actually want a progress bar which move continually with the player movement either it is straight path or a curve... Thank for showing your interest
Thought so. Unfortunately, the only thing i can think of, due to my lack of experience in racing games, is using large colliders. Each piece of the track would have a collider covering all of the usable area (the terrain the car can go on), then either in Update or FixedUpdate, check per section car position. That way you would be able to use local coordinates to know how much a car has moved and in which direction. That way you might even know when it's going backwards. Since this would need to be applied to each section, you would actually just need one script to do a check for the object it's attached to. So you could use a var that says how much of the section was covered by the car. Then you can have another script that has an ordered list of sections, and it will check for each section which car has advanced how much.
Using checkpoints you can store the last checkpoint and find next one. Having distance between both and knowing car's position you can "fake" continuous progress.
Another solution could be using Bezier curve(s). You should define your track as Bezier curve (or set of them), then you will be able to find car position on the track by casting actual car position. After all, you can compute distance between projected point and the end of the track: I think that's the approach for racing games. Check the link:
Other solution I can think about is to use built-in Nav$$anonymous$$eshAgent. It has remainingDistance, but I'm not sure if it will work without setting the position using Nav$$anonymous$$esh.