- Home /
The question is answered, right answer was accepted
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.
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.
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) , 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.
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.
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
Thx very much, your answer helped me a lot. Sorry for late response, I had hard times at school :).
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
Thanks your answer worked beautifully and its very simple.
it works amazingly well man. have tried almost all the solutions and finally this is working. Thanks a lot :)))
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
}
Follow this Question
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