Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 02, 2017 at 05:15 PM by Voky for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Voky · May 20, 2017 at 08:51 AM · c#2draycast

Button blocking raycast

Hi, I am making 2D game and I have a script, that on screen touch makes raycast hit and where the hit end up, there will be spawned an game object. So, my problem is that I have some buttons on the screen and when I press one of the buttons, raycast go through it and game object will spawn anyway.. I want to avoid that, because I don't want to spawn the object behind the button. So, is there any way, how can Image (button) component stop raycast? Box collider as child of the button isn't the wisest solution, I tried that and it wasn't very handy in future progress.

Thanks for any kind of help.

Comment
Add comment · Show 3
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 Vandarthul · May 20, 2017 at 09:16 AM 0
Share

Did you try to bool-check your raycast? Assign a bool that will change according to button's existence?

But to me that's a wrong way to do it. Coupling your game logic with UI is not a way to go. I don't exactly know what you are trying to accomplish but you should not be in a situation where your game logic(spawning a game object in this case) coupled with UI state. You can explain further what you are trying to accomplish and we can help, or you can re-think your way of doing things. Just my advices for you.

avatar image Voky Vandarthul · May 22, 2017 at 08:49 PM 0
Share

Thx for reply, I think I didn't described my problem well, so I will try to explain it again (sorry, bad english skills :) ).

So, I am making a game where the player can build his own castle from 2D blocks and I have a script that on Input.$$anonymous$$ouseButtonDown instantiates the blocks. Problem appears when I want to click on button, which is next to the instatiated block (World Space canvas) alt text , because it will spawn another block ins$$anonymous$$d of activating the button. So, is there way, how can button stop raycast hit going trough and ins$$anonymous$$d of it activate itself (Button)?

Thx, for help.

avatar image SohailBukhari · May 23, 2017 at 06:38 AM 0
Share

Draw Rectangle and before spawning check whether touch is in area of rectangle or not.

         private var touch1 : Vector3;
     private var touch2 : Vector3;
     var rect1 : Rect; // Rectangle To Draw On Screen
 
 function  Start()
     {
         rect1 =  Rect( getX(0), getY(200),getX(290),getY(280)); // draw according to your screen resolution
     }
 
 for ( var i : int  = 0 ; i < Input.touchCount  && Input.touchCount == 1  ; i++ ) 
             {
                   var touch: Touch = Input.GetTouch(i);
                   
                   
                      
                  if(!rect1.Contains(Vector2( touch.position.x,Screen.height - touch.position.y))
                  {
                      
                      // here spawn object
         }

There will be syntax errors in the code , i just wrote code for your understanding.

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by zzeeshann · May 22, 2017 at 11:27 PM

use this method below for preventing UI button click passing through.

      //When Touching UI
     private bool IsPointerOverUIObject()
     {
         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
         eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         List<RaycastResult> results = new List<RaycastResult>();
         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
         return results.Count > 0;
     }

Usage :

 if (!IsPointerOverUIObject())
  {
        Debug.Log("Stop");
  }

For more detail check this video. https://youtu.be/QL6LOX5or84

Comment
Add comment · Show 1 · 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 Voky · Jun 02, 2017 at 05:15 PM 0
Share

Thx very much, your answer helped me a lot. Sorry for late response, I had hard times at school :).

avatar image
1

Answer by TheSmokingGnu · May 25, 2017 at 02:53 PM

You can always chek if user clicked(touched) a point thats inside button, by putting this in the beginning of an update method just after the checking for input part:

if (RectTransformUtility.RectangleContainsScreenPoint( yourButton.GetComponent(), Input.mousePosition)){ //ignore the click }

here is the documentation for the funcrion used: https://docs.unity3d.com/ScriptReference/RectTransformUtility.RectangleContainsScreenPoint.html

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 phillip71newman · May 16, 2018 at 01:14 PM 0
Share

Thanks your answer worked beautifully and its very simple.

avatar image divich · Oct 22, 2021 at 09:54 AM 0
Share

it works amazingly well man. have tried almost all the solutions and finally this is working. Thanks a lot :)))

avatar image
1

Answer by AmbushSky · May 23, 2017 at 08:46 AM

Include the following namespace:

 using UnityEngine.EventSystems;

Include this as a check for the action being completed:

 if (EventSystem.current.IsPointerOverGameObject()
 {
     // Do nothing
 }
 else
 {
     Perform action here
 }
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

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Getting a Raycast rope to follow its object 0 Answers

Multiple Cars not working 1 Answer

Physics2D.Raycasting questions 0 Answers

Raycast for a 2D objects 0 Answers

Raycast2D always "remembers" it's last hit 2 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