- Home /
Detect touch on a specific GUITexture
Greetings, guys. While i'm working on my Unity3d Android project, i bumped into one thing, that I can't do properly. I'm creating an interface, and I have 2 GUITextures, each one represents a button. UP and DOWN. When I tap on one, animation plays well, but Unity does not count another GUITexture as separate button. So when I press it, it works like first button. Here is some code:
Code for button UP
function Update () {
if(Input.touchCount >= 1)
{
var touch : Touch = Input.touches[0];
if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
animation["UP"].speed= 1.0;
animation.Play("UP");
}
else
{
animation["UP"].speed= 0;
}
}
}
Code for button DOWN
function Update () {
if(Input.touchCount >= 1)
{
var touch : Touch = Input.touches[0];
if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
animation["UP"].speed= -1.0;
animation.Play("UP");
}
else
{
animation["UP"].speed= 0;
}
}
}
Im fact, I know, that i need to use "hit.test" method, but I dunno how to make it right. Thanks for your attention
Answer by dannyskim · Feb 09, 2012 at 06:43 PM
function Start()
{
var hit : GUILayer = Camera.main.GetComponent(GUILayer);
}
function Update()
{
var hitObject : GUIElement = hit.HitTest( Input.mousePosition );
if( hitObject != null )
{
switch( hitObject.name )
{
case "guiTexture_name1":
// do stuff
break;
default:
break;
}
}
}
dannyskim, your method does not work fine. Script plays animation "UP" when i tap any part of my screen.
How are you using it? You need to have at least two objects, and you need to name them uniquely. What part of the code isn't working?
Well, I have 2 GUITextures called "up" and "down". I attach this script to each one, but change this line: case "guiTexture_name1" on case "up" and case "down".
case "up": animation["UP"].speed= 1.0; animation.Play("UP");
case "down": animation["UP"].speed= -1.0; animation.Play("UP");
But script "down" doesn't work at all and if I press anywhere on my touchscreen - animation UP start playing.
Rocotos code provided by dannyskin worked fine. You must check your code before commenting, Check out what you've wrote..
case down - you are playing same animation as in case up. So how do you expect that it will work fine?
Answer by jacobschellenberg · Feb 09, 2012 at 08:02 PM
If you use the GUI.Button you will have more control over what buttons are being touched. The buttons will automatically react to touch inputs, which is really nice because you don't have to constantly be checking for a touch input. This is what I would do.
void OnGUI(){
if(GUI.Button(new Rect(0,0,50,30), "Up"){
//play animation up
}
if(GUI.Button(new Rect(0,40,50,30), "Down"){
//play animation down
}
}
It is pretty much recommended across the board to not use OnGUI in any mobile app / game developed in Unity. There is way too much overhead associated with this method. If you are going to use Unity's GUI System, the only reasonable option for mobile platforms is using GUITextures as they are considered GUIElements. GUIElements are not associated with OnGUI and thus do not exhibit the overhead of OnGUI.
Answer by Rocotos · Feb 11, 2012 at 03:15 PM
I have another question (as I deal with previous). This script works only once, I mean when I want to play animation again and tap the button...nothing happend. What I need to add to my script?
function Update() {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, hit, 100))
{
if (hit.collider.gameObject.tag == "newgame")
{
animation.Play("newgame");
}
}
}
Your answer
Follow this Question
Related Questions
Android Touch Input GUITexture 2 Answers
Move guiTexture along touch position 0 Answers
GUITexture touch play animation 0 Answers
GUITexture Touch Problem 2 Answers
GUITexture Button Help, Android touch 2 Answers