Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chappy · Mar 14, 2014 at 07:43 PM · c#touchpause

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 14, 2014 at 07:53 PM 0
Share

Can you post the code that calls OnClick? You get Touches when the timeScale is 0...

avatar image Chappy · Mar 14, 2014 at 08:02 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chappy · Mar 14, 2014 at 08:23 PM 0
Share

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.

avatar image whydoidoit · Mar 14, 2014 at 08:24 PM 1
Share

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.

avatar image Chappy · Mar 14, 2014 at 08:59 PM 0
Share

Hmm, good to know, I have been misinformed. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to create a shoot button for touch GUI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges