- Home /
How to exclude OnGUI button from RayCastHit?
Hi guys, I control a player by touching the screen using Ray. I also have a button for firing named "Cube". I excluded this cube from the ray like this:
if (rayCastHit.transform.gameObject.name != "Cube")
It causes that I can fire without moving the player. Now I want the same with a GUI button ("Paused"/"Resume" - two switching buttons on the same position). How can I exclude this buttons so it is out of the ray? Whitch condition do I have to add?
Thanks
Are you saying that you don't want to raycast when you click a button?
Answer by hav_ngs_ru · Jan 28, 2015 at 10:38 AM
if (rayCastHit.transform.gameObject.name != "Cube"
&& rayCastHit.transform.gameObject.name != "Paused"
&& rayCastHit.transform.gameObject.name != "Resume") {
...
}
No luck. The GUI is not a game object. It is just script. I add this button to the screen like this:
transform.gameObject.AddComponent<PauseButton>();
I enclose the whole script:
using UnityEngine;
using System.Collections;
public class PauseButton : $$anonymous$$onoBehaviour {
public string buttonToken = "Resumed";
public const string pausedToken = "Paused";
public const string ResumedToken = "Resumed";
void OnGUI()
{
const int buttonWidth = 90;
const int buttonHeight = 25;
switch (buttonToken)
{
case ResumedToken:
{
if (GUI.Button (new Rect (Screen.width - (buttonWidth + 30), Screen.height / 10 - buttonHeight,
buttonWidth, buttonHeight), "Pause"))
{
buttonToken = pausedToken;
Time.timeScale = 0f;
}
break;
}
case pausedToken:
{
if (GUI.Button (new Rect (Screen.width - (buttonWidth + 30), Screen.height / 10 - buttonHeight,
buttonWidth, buttonHeight), "Resume"))
{
buttonToken = ResumedToken;
Time.timeScale = 1f;
}
break;
}
default:
{
break;
}
}
}
}
In this case you could just read buttons condition and dont raycast if some of them pressed.
Hi, thank you for your advice. How could I do it, in the script?
I wouldnt write all code for you, but solution for detecting "click" and "hold" conditions is wrote here for example: http://answers.unity3d.com/questions/861338/guirepeatbutton-returns-true-false-true-false.html
the @AdamScura's answer contains more applicable code example for your case.
Answer by gheeler · Jan 29, 2015 at 10:05 AM
public class GUIManager : MonoBehaviour{
public static bool InputOverGUI (Rect[] guiRects) {
inputPos = new Vector2[Input.touchCount];
for (int i=0; i<Input.touchCount; i++) {
inputPos[i] = Input.touches[i].position;
}
foreach (Vector2 v in inputPos) {
foreach (Rect rect in guiRects) {
if (rect.Contains(v))
return true;
}
}
return false;
}
}
This function will check each touch and if any touches are in any guiRects it will return true otherwise it will return false.
This isn't tested so you may need to debug... You may have to invert the y of the input i.e.
inputPos[0].y = Screen.height-inputPos[0].y
Well, I tried, but I am just a beginner and your code is too complex to me for now. I thought only to add another condition will do the trick... Sorry.
Your answer
Follow this Question
Related Questions
GUI Follow RaycastHit 2 Answers
Making raycasts go to the centre of the screen 1 Answer
Issue with raycasting in 2D 1 Answer
How to render a colored 2D rectangle. 6 Answers
Unity Physics2D.Raycast always returns “the invoker” as hit 2 Answers