Object Still Moving at Same Position After Respawn

I tried to be a bit creative and added moving walls with Mathf.Sin but when the game reloads the scene it is still moving at the same position.
ezgif-5-a759d094e1

Hi,

Welcome to our community! :slight_smile:

First of all, good job on challenging yourself.

Regarding the problem, given you use Rick’s code ‘as it is’, you use Time.time. This is the time since the game started, not since the level started. If you want to ‘reset’ the value, you need a variable, which stores the Time.time value when the level gets started. If you reload the level, you could simply use the Start() method.

Time.time - yourVariable should be 0 when the level starts.

I hope you understand what I mean. I don’t want to tell you the whole solution right away because you might want to try to solve this problem yourself. You learnt everything you need to write the code. The rest is just logic. :slight_smile:

2 Likes

Thank you for the reply, I managed to get it working!

image

Even though I got it working at first, I still couldn’t understand the logic but using debug.log really helps a lot as I can see how the two values actually help to reset not the position of the obstacle but the initial wave value instead. :exploding_head:

Good job! That’s exactly what I meant. :slight_smile:

Your expecation was that everything ‘restarts’, which means that you expected the values to be reset when you load a new level. However, unlike other things, Time.time does not get reset. That’s why you need this little workaround to get 0f when the level starts. You achieved that with Time.time - startTime. That’s the logic behind your solution, and that’s hopefully what you saw when you used Debug.Log.

Debug.Log is our most important tool in Unity to debug our games. In many cases, problems become obvious once you make values appear in your console. Do use it whenever you want to figure out what’s going on at runtime. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms