- Home /
Raycasting/Touches with timescale 0, C#.
I'm working on a basic pause menu script for mobile. I've currently got it so that a touch sends out a raycast towards the pause button or the menu buttons. When the pause button is hit by the raycast the timescale becomes 0. The problem is, once the timescale is set to 0 no more touches are detected and I need them to be. I've tried setting the timescale to something really low instead of 0 and that didn't help.
void OnClick()
{
if (this.gameObject.name == "Pause_Button")
{
PauseMenu.SetActive (true);
Time.timeScale = 0;
}
if (this.gameObject.name == "Resume")
{
PauseMenu.SetActive (false);
Time.timeScale = 1;
}
if (this.gameObject.name == "Quit")
{
Application.LoadLevel (0);
}
if (this.gameObject.name == "Options")
{
}
}
Looking for nay suggestions on how to fix this/work around this. A way to pause the game without changing the timescale or something like that.
EDIT: Code that sends OnClick as requested.
void FixedUpdate ()
{
if ( Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.Android )
{
if ( Input.touchCount <= 0 )
return;
// detect single touch only
Touch touch = Input.touches[0];
if ( touch.phase == TouchPhase.Began )
{
Debug.Log( 123 );
OnTouchBegan( touch.position );
}
}
else
{
if ( Input.GetMouseButtonDown( 0 ) )
{
Debug.Log(Input.mousePosition);
OnTouchBegan( Input.mousePosition );
}
}
}
void OnTouchBegan (Vector2 screenPos)
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
RaycastHit hit;
if ( Physics.Raycast( ray, out hit ) )
{
hit.collider.gameObject.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
}
}
}
SECONDARY EDIT: Added the Debug.Log code back into it.
Can you post the code that calls OnClick? You get Touches when the timeScale is 0...
Sure, added to the main post.
I thought that as well, I could have sworn I had read that timescale didn't do anything to that but there was a Debug.Log in the code that I deleted that detected the touch position and after I pressed the pause button it stopped sending the message to the console.
Answer by whydoidoit · Mar 14, 2014 at 08:20 PM
Ok so don't do raycasting in FixedUpdate - the reason is that this only runs when the physics system detects that it needs to catchup with the current frame being rendered. If the timeScale is 0 then there is no need to run physics steps and so your code isn't executing.
If you move your raycasts to Update then it will function correctly.
I hate when the answer is that simple. This was code that I got from someone else I was working with so I just kind of went with what they gave me.
Well, it works now, thank you very much. Didn't know that about FixedUpdate, all I knew was that it update at fixed intervals not every frame.
Yeah it doesn't run at fixed intervals (common misconception) it runs at the start of a real frame (however fast that is) but it runs multiple times with the fixed time moving on by the fixed step until it catches up with the current frame time.
Your answer
Follow this Question
Related Questions
disable touch when game is paused(Time.timeScale = 0) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnTouch Problems :( (C#) 0 Answers