Im making a side scrolling shooter, and when the player dies they press R to restart and the scene and enemies restart. But the problem is not everything in the scene is reset, I have a scrolling background and this doesnt reset to the starting position.
This is the script for the scroller:
using UnityEngine;
using System.Collections;
public class BGScroller : MonoBehaviour
{
public float scrollSpeed;
public float tileSizeZ;
private Vector3 startPosition;
void Start()
{
startPosition = transform.position;
}
void Update()
{
float newPosition = Mathf.Repeat (Time.time * scrollSpeed, tileSizeZ);
transform.position = startPosition + Vector3.forward * newPosition;
}
}
eses
January 22, 2017, 1:42pm
2
@serbusfish
Hi there - It’s as simple as this: Reload your whole scene!
I don’t think there is very often need for “resetting” a scene manually, unless it’s very complex.
tomicz
January 22, 2017, 2:01pm
3
You can do this. You do not reset scene manually unless it’s something specific.
using UnityEngine.SceneManagement;
if(playerIsDead)
{
SceneManager.LoadScene(SceneManager.GetActiveScene()) ;
}
1 Like
I am currently using:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Everything in the scene resets except the background which doesnt return to the starting position.
I recorded a quick clip. You will see at 0.19 seconds in when I reload the scene the background asteroids dont reset:
steego
January 22, 2017, 4:13pm
5
This is because you use Time.time, which will not be the same every time you start the scene.
Instead create your own timer variable, and increment it with Time.deltaTime each frame.
Something like this:
using UnityEngine;
using System.Collections;
public class BGScroller : MonoBehaviour
{
public float scrollSpeed;
public float tileSizeZ;
private float _timer = 0f;
private Vector3 startPosition;
void Start()
{
startPosition = transform.position;
}
void Update()
{
_timer += Time.deltaTime;
float newPosition = Mathf.Repeat (_timer * scrollSpeed, tileSizeZ);
transform.position = startPosition + Vector3.forward * newPosition;
}
}
3 Likes
steego:
This is because you use Time.time, which will not be the same every time you start the scene.
Instead create your own timer variable, and increment it with Time.deltaTime each frame.
Something like this:
snip
Ah right now I understand, thanks a lot!
Ok this
if input.get
if (Input.GetButton (“Restart”)
{
SceneManager.LoadScene(SceneManager.GetActiveScene()) ;
}
serbusfish:
I am currently using:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Everything in the scene resets except the background which doesnt return to the starting position.
I recorded a quick clip. You will see at 0.19 seconds in when I reload the scene the background asteroids dont reset:
https://www.youtube.com/watch?v=TPLZJnCJ9Ck
There could be static variables. You have to set initial values before load scene
3 Likes
Thanks this helped me lot