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 CodingNoob · Mar 19, 2015 at 02:33 PM · uimouse position

How to prevent character from jumping, when clicking pause-button

Hey there,

I got a little issue again: I created a Pause-Button which stops the game through changing the Time.timescale, but by clicking the button, my character jumps (jumping is forced by Input.GetButtonDown ("Fire1")), and when clicking the resume-button the game, he does his doublejump. So for that reason, I need to somehow "deactivate" the Input of the active game while hovering over or by clicking the button. The game is supposed to run on mobile phones also, so it needs to know that over the buttons, the player controls are deactivated...I found some suggestions but none of them really work for me somehow :( (like for example: http://forum.unity3d.com/threads/4-6-how-to-detect-if-any-gui-element-has-been-clicked-on.263473/)

Thanks in advance

best regards

Comment
Add comment · Show 6
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 CodingNoob · Mar 19, 2015 at 02:50 PM 0
Share

For when the pause menu is up, I can control the playercontrols via a bool which prevents him from jumping until i clicked resume. But the true problem is to "define" the area of the pause button so that in this area the input does only count for the UI...

avatar image hexagonius · Mar 19, 2015 at 02:52 PM 0
Share

The solution melange brought up in your posted link sounds promising to me. What's wrong with the post?

avatar image CodingNoob · Mar 19, 2015 at 02:59 PM 0
Share

If I got it right, it just checks if the pointer is over the button - basically, yes, that's exactly what I'm looking for, but with touch, I don't have a cursor...or am I getting something wrong??

avatar image dr3th · Mar 19, 2015 at 03:30 PM 0
Share

Around your Input.GetButtonDown("Fire1") function add

if(NotPaused()) { Input.GetButtonDown("Fire1") {

} }

Create a function that represents boolean paused State

Public bool NotPaused() { //SomeCodeThatWillrepresentThePausedState;

return false; }

avatar image Xymbiot3 · Mar 20, 2015 at 04:06 PM 0
Share

Another way to approach this is to build a state stack class that holds an enum with the defined states. Then whenever you enter a state just push it to the stack and have the checking code like dr3th mentioned above. You'll want the stack class to basically be a non-destroyed singleton. This solution is a little more extendable compared to a single boolean. Just some pseudocode:

 public enum GameState {
   InGame,
   Paused
 }
 
 public class GameState$$anonymous$$anager {
 
   private Stack<GameState> _gameStack;
 
   // This is simplified for example, but this would have 
   // singleton code as well
   void GameState$$anonymous$$anager() {
     _gameStack = new Stack<GameState>();
   }
 
   public GameState GetGameState() {
     return _gameStack.Peek();
   }

   public void PushState(GameState state) {
     _gameStack.Push(state);
   }

   public void PopState() {
     _gameStack.Pop();
   }
 }
 
 public class $$anonymous$$yInputClass {
   private GameState$$anonymous$$anager _stack;
 
   void Start() {
     _stack = GameObjects.Find("GameStackObject").GetComponent<GameState$$anonymous$$anager>();
     _stack.PushState(GameState.InGame);
   }
 
   void Update() {
     switch(_stack.GetGameState()) {
     case GameState.InGame:
       //Example purposes
       if(Input.GetButtonDown("Fire1") {
         Jump();
       }
       if(Input.GetButtonDown("PauseButton") {
         _stack.PushState(GameState.Paused);
       }
       break;
     case GameState.Paused:
       DoPauseStuff();
       if(Input.GetButtonDown("PauseButton") {
         _stack.Pop();
       }
       break;
   }
 }

Again this is all written without a compiler so part of it is pseudocoded based on memory, my apologies if parts aren't syntactically correct.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ParanoidSnail · Mar 25, 2015 at 09:21 AM

The way dealt with mouse over ui, i have an array, that contains my UI elements that i do not want to let the click go through; I assigned the UI elements in the inspector.

 //Holds all ui elements that don't let the touches/clicks pass through to the scene
 public RectTransform[] uiElements;

 /**
  * Tests if the mouse button is over UI
  */
 public bool isMouseOverUI() {
     Vector2 mousePosition = Input.mousePosition;
     foreach (RectTransform elem in uiElements) {
         if (!elem.gameObject.activeSelf) {
             continue;
         }
         Vector3[] worldCorners = new Vector3[4];
         elem.GetWorldCorners(worldCorners);
 
         if(mousePosition.x >= worldCorners[0].x && mousePosition.x < worldCorners[2].x 
            && mousePosition.y >= worldCorners[0].y && mousePosition.y < worldCorners[2].y) {
             return true;
         }
     }    
     return false;
 }

When jumping, just test first for isMouseOverUI();

Alternatively you can play around with

 EventSystem.current.IsPointerOverGameObject()

http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html But i found that it did not work correctly with touches (on android device). This was in 4.6 beta, haven't tried to use it again yet, maybe it was fixed.

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image

Related Questions

How could I restrict the mouse movements in a specific area ? 1 Answer

Mobile controls, gui mouse problem. 0 Answers

GameObject rapidly changing position (flickering) 1 Answer

How do I create an Exit confirmation Pop up when I click the exit button? 4 Answers

Restrict mouse click area 3 Answers


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