- Home /
Move an object from start to end position depending on the progress of loadlevel..async
Hi,
I'm using loadlevel.async to load my game from the start screen. Whilst the level is loading I would like an object to be set to active which moves from a set start to finish position - but does this depending on the progress of the loading. (So it kind of works like a progress bar, where a 20% fill is 20% loaded level -- but instead I'd like the 20% fill to mean the object has moved 20% across this set distance.
It doesn't have to be super exact - just roughly right.
However - I'm having two problems. The first is that in my console Debug.Log(async.progress) only ever reads 0, and then 1 on loading. (I read online that if I multiply this by 100 this would sort out the issue of it rounding the float number, but I'm still not seeing any change.
Secondly: does anyone have any pointers on how I would achieve this, I'm a bit stuck!
void OnSwipeUp()
{
StartCoroutine (LoadMainLevel());
}
public IEnumerator LoadMainLevel()
{
AsyncOperation async = Application.LoadLevelAsync(1);
float t = async.progress;
overlayTerrain.SetActive (true);
overlayTerrain.transform.position = Vector3.Lerp(startPos, endPos, t);
Debug.Log (async.progress *100f);
yield return async;
}
}
Best, Laurien
I don't know anything about AsyncOperation, but I do know how move an object from start to end based on your percentage.
Use Vector3.Lerp: https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
progressBar.transform.position = Vector3.Lerp(startPosition, endPosition, loadProgress);
"loadProgress" is a percentage but must be between 0 and 1.
Answer by SarperS · Jun 22, 2016 at 04:49 AM
private IEnumerator LoadScene(string sceneName) {
// Wait a single frame so the frame gets rendered (required if this method is called in Awake or Start)
yield return null;
// Start the loading operation
var loadingOperation = Application.LoadLevelAsync(sceneName);
while(!loadingOperation.isDone) {
overlayTerrain.transform.position = Vector3.Lerp(startPos, endPos, loadingOperation.progress);
yield return null;
}
yield return loadingOperation;
}
Hi - that's great thanks!
I'm still having the problem that the .progress is reading 0 all the time while the level is loading, so the game object doesn't move until the last frame when the level has loaded.
Have you had any problems with the .progress not working?
Nope, works fine on my end. One possible problem that I can think of would occur if you invoke this coroutine multiple times. Try this to run it only once;
private IEnumerator loadCoroutine;
void OnSwipeUp() {
if (loadCoroutine == null) {
loadCoroutine = LoadScene("Your_Scene_Name_Here");
StartCoroutine(loadCoroutine);
}
}
That's great - it's working (in a build rather than through Unity remote).
It seems to be a bit unreliable - it goes slowly from 0 - 0.3 and then jumps to 0.9, so I might have to fake it by estimating the time ins$$anonymous$$d?
Your answer
![](https://koobas.hobune.stream/wayback/20220612083417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loading Bar in Different Scenes 1 Answer
How to load levels with animation GUI 1 Answer
Loading Bar for Awake Function 1 Answer
LoadLevelAsync doesn't work as it should 2 Answers
How works loadLevelAsync 0 Answers