- Home /
Making a GUI text disappear with a tap, FPS look around with finger
Hi, I have this script, which when a specific object is tapped, it creates a nice GUI textbox. The issue is, I then want the textbox to disappear when tapped, or when the screen is tapped elsewhere. I've had no luck in figuring it out.
var turret: GameObject; private var showBox = false; var boxwidth: int = 300; var boxheight: int = 300; var boxfromleft: int = 500; var boxfromright: int = 500;
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray)) {
showBox = true;
}
}
}
}
function OnGUI () { if (showBox)
GUI.Box (Rect (boxfromleft,boxfromright,boxwidth,boxheight), "A classic Neo-Victorian Turret");
}
Also, I also I have been trying to get a first person look around script, that when a camera in enabled in the first person view, dragging a finger around the screen allows you to look around. I've tried modifying the first person iPhone controls, to just have the right side joystick working because thats all I need.
Any help is much appreciated! Thanks K.
Answer by Blankzz · Aug 08, 2011 at 02:09 PM
My answer is related to your first question. Maybe you should split your questions into two posts. Anyway..... Have you tried storing the textbox rectangle and using Rect.contains to see if the mouse is within the textboxs' rectangle or out of the rectangle when the mouse is pressed or finger taps? Look at http://unity3d.com/support/documentation/ScriptReference/Rect.Contains.html
Edit: Try this code and check logging to see what is happening
var rect = Rect(boxfromleft,boxfromright,boxwidth,boxheight);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (showBox == false)
{
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
var hit: RaycastHit;
if(Physics.Raycast (ray,hit,10000) && hit.collider.gameObject.tag == "rightturret")
{
showBox = true;
Debug.Log("Box should be displayed");
}
}
else
{
if (!rect.Contains(Input.GetTouch(0).position))
{
showBox = false;
Debug.Log("Box should be hidden");
}
}
}
That seems logical, but for some reason when I execute the code, Unity is telling me that 'touch' is and unknown identifier. So something to do with the "Rect.Contains(touch.position)" line.
Worked Wonderfully. I noted the addition to the "if (showBox == false)" statement earlier in the block of code, because indeed before it worked I was getting, errors related to the Boolean.
Thanks very much for your help, I'll organize my code better next time as well! $$anonymous$$
Answer by kohenb · Aug 09, 2011 at 04:07 PM
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position); var hit: RaycastHit; if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { if (Physics.Raycast (ray,hit,10000)) if (hit.collider.gameObject.tag == "rightturret") { showBox = true;
var rect = Rect(boxfromleft,boxfromright,boxwidth,boxheight);
if (rect.Contains(Input.GetTouch(0).position))
{
showBox = false;
}
}
}
}
I've updated to the following code, with no errors, however it will not make the GUI.box disappear yet. Thoughts?
Thanks again for your input! K.
Could you make sure your code is formatted correctly before posting. I'm now going to update my post with code that I think is better. I'm not sure why its not disappearing but lets try troubleshoot.
Your answer
Follow this Question
Related Questions
Lighting not working on iOS device with fpc 0 Answers
How to use FirstPersonController script for iPhone? 2 Answers
iphone/ipad Gyro perpendiculat and moving FPC 1 Answer
Switching between FPS and Relative during play? 1 Answer
Controller for iPhone 2 Answers