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 Watsong · Dec 07, 2014 at 11:07 PM · c#guicharacteriphone

How to prevent world interaction when using UI elements?

Hello,

I'm making a mobile game. During the game, on the screen there are some UI buttons. If the player presses on a button, the button is pressed and an event occurs.

If the player presses on the screen, the player's character jumps.

The problem is - when pressing a UI button, the character jumps as well - because screen touches must be taken as an input for this purpose.

How can I prevent the player's character from jumping, if the touch screen press is made on a button, and not on any other part of the screen? I saw a function ".IsPressed" that the "Button" component inherits. However, this is a protected function for triggering an event...

I need some way to either block out part of the screen from causing the character to jump or some way of preventing the jump action from triggering when pressing on a UI button. But there doesn't seem to be any way of getting the state of a button.

I'd be grateful for any help.

Thanks.

Comment
Add comment · Show 1
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 jenci1990 · Dec 07, 2014 at 11:54 PM 0
Share

http://answers.unity3d.com/questions/847119/how-to-replace-button-text-when-clicking-another-b.html

2 Replies

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

Answer by Kiwasi · Dec 11, 2014 at 06:18 PM

The best way to do this is to convert all your world clicks into events, using the Unity event system. This means the event system will automatically prioritise the clicks based on the distance from the camera. You will need to add a physics raycaster to your camera, and implement the various event interfaces on your scripts.

As an alternative you can check EventSystem.current.IsPointerOverGameObject

Comment
Add comment · Show 2 · 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 Watsong · Dec 12, 2014 at 03:59 AM 0
Share

Thanks for your reply.

I'll look into this and get back to you. I get the impression that this will work like on the "Survival Shooter" tutorial project. That is, clicking or touching the screen 'shoots' a ray that hits the first object in a line from a given point, (such as the camera origin).

avatar image Watsong · Jan 15, 2015 at 09:30 PM 0
Share

"EventSystem.current.IsPointerOverGameObject" - This only works for the mouse, unfortunately. I am implementing touch screen as well. Touch screen activates "IsPointerOverGameObject" only while the touch is held on the screen, which isn't sufficient.

I tried raycasting. However, the raycasts go through the UI objects, so I had to hunt for a workaround.

Thank you for your support. Question answered.

avatar image
0

Answer by static_cast · Dec 08, 2014 at 12:00 AM

You could check this...

 bool mybuttonisbeingpressed
 
 if(!mybuttonisbeingpressed)
  jump
 
 
 void onpress()
 { mybuttonisbeingpressed = true }
 
 void offpress()
 { mybuttonisbeingpressed = false }

This is psuedo-code, as I haven't gotten the chance to program much with the 4.6 UI, but it's a solution that works. :P

Comment
Add comment · Show 2 · 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 Watsong · Dec 08, 2014 at 12:14 PM 0
Share

Thanks for your reply.

The problem with this solution is that it works only if there is just one button the screen, as it takes the 'jump' function away from the character control script. A workaround would be to have a variable changed in another script that holds the jump function.

But!, I still can't find a function or value related to touchscreens - for 'pressed' and 'unpress' on buttons. I tried attaching a script to the button and using "On$$anonymous$$ouseDown", for example, and this didn't work at all, (Debug log used to write a message if successful).

"IsPressed" and "OnPointerDown" both require eventData which the manual describes as "The EventData usually sent by the EventSystem.". Going through the manual to try and find how to provide said EventData manually leads to a HTTP404.

I'm now looking at the "Touch" structure to see whether I can find a solution through that.

avatar image Watsong · Dec 11, 2014 at 04:26 PM 0
Share

I found a low quality workaround that is based on the fact that mouse and touch position begins at 0,0 in the bottom left of the screen. If all GUI elements are anchored to the bottom left of the screen, it is possible to isolate that portion of the screen from triggering in world interaction.

I have noted the poor touchscreen support that Unity provides, should I be considering Unity for future mobile game projects.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How would you divide an object into its faces? 2 Answers

Distribute terrain in zones 3 Answers

Unity Invoke method delay factor vary device to device 1 Answer

Multiple Cars not working 1 Answer

C# Movement Script 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