- Home /
Question by
imLor · Oct 16, 2021 at 06:42 PM ·
2d game2d-physics
Loading a scene with a long press!
Hey! Things to add to the code. I want to load the scene after 5 seconds of holding! Can you tell me Please?
public string [] SceneNames;
void OnMouseDown ()
{
int s = Random.Range (0, SceneNames.Length) ;
SceneManager.LoadScene(SceneNames[s]);
}
Comment
Best Answer
Answer by Hellium · Oct 16, 2021 at 06:50 PM
private float mouseDownTime;
void Update()
{
bool hasBeenClicked = mouseDownTime > 0;
if(hasBeenClicked)
{
bool timeHasElapsed = Time.time > mouseDownTime + 5;
if(timeHasElapsed)
{
int s = Random.Range (0, SceneNames.Length) ;
SceneManager.LoadScene(SceneNames[s]);
}
if(Input.GetMouseButtonUp(0))
{
mouseDownTime = -1f;
}
}
}
void OnMouseDown ()
{
mouseDownTime = Time.time;
}
Your answer