Unity GUI + iPhone Touch = Need Lots of Help Please!

Hi Guys!

I have searched this forum and the Script Reference and Google with no solution.

Problem: I can’t seem to get my GUI textures that I created in Photoshop (Up arrow, Down arrow, Left Arrow, Right Arrow) to control my player’s movements when the user touches the arrows on their iPhone / iPod Touch screen.

The iPhone Touch control code examples in the Script Reference allow me to drag something around or trigger some action when the user touches the screen anywhere, but I don’t want the user to just touch “anywhere”, I want them to have to touch the button (GUI texture) that I create before something will happen. Am I missing something here or is this not possible to do with Unity iPhone yet?

It seems like a rather simple thing to do… Create a button and when the user touches it have your game object do something… what could I be doing wrong here? Do I have to use a Unity GUI Button / Skin instead of my own GUI Textures?

Any help or guidance would be greatly appreciated.

Thanks!

Joey

You can use GUITextures.
Just take the rect you use on it and check if the touch is in the rect. If so then the “button is beeing pressed” and you execute the corresponding movement.

This should get you going nicely

http://forum.unity3d.com/viewtopic.php?t=16373

Thanks for the quick response! You guys are good!

I will have to really study the scripts on that link because I’m not sure how to implement them into my game as I don’t code in C# and am entry level at best in JavaScript.

What “rect” are you referring to? The rectangular shape of my gui texture? Do I need to put my gui textures inside of individual game objects in order for this to work?

I finally got it to work, although now I have other issues to deal with like whenever my player character hits anything else in the scene he spins out of control, but at least I got the arrow buttons to work to make him move. Here is the code I pieced together and did my best at documenting so as to help the next poor soul who attempts doing this with little to no programming experience…

// declare all variables and remember to drag and drop your Player character Rigidbody and GUITextures into the slots in the Unity Inspector window.
var Player : Rigidbody;
var Speed : float = 200;
var ArrowUp : GUITexture;
var ArrowDown : GUITexture;
var ArrowLeft : GUITexture;
var ArrowRight : GUITexture;



function Update () {
	
// begin "for" loop to check for iPhone touches	and create a variable "evt" to store them in
   for (var evt : iPhoneTouch in iPhoneInput.touches) 
   { 
       
// hit tests: create variables to store hit test results then test for hit inside each GUITexture
    var myUpHitTest = ArrowUp.HitTest(Input.mousePosition);
    var myDownHitTest = ArrowDown.HitTest(Input.mousePosition);
    var myLeftHitTest = ArrowLeft.HitTest(Input.mousePosition);
    var myRightHitTest = ArrowRight.HitTest(Input.mousePosition);  
         
// specify which type of iPhone Touch (Began, Stationary, Ended, etc.) to use. I chose "Stationary" so as long as I press and hold on the GUITexture button my player target moves     
      if (evt.phase == iPhoneTouchPhase.Stationary) 
         { 
         	
         	// nested if statements that tell what to do if each hit test is true 
         	
         	// make Player move up if the "ArrowUp" GUITexture button is pressed
                if(myUpHitTest) 
               {  
               	Player.AddForce (0,Speed * Time.deltaTime, 0);     
                  
               }
               
             // Make Player move down if the "ArrowDown" GUITexture button is pressed    
               if(myDownHitTest) 
               {  
               	Player.AddForce (0,-Speed * Time.deltaTime, 0);     
                  
               }
               
              // Make Player move left if the "ArrowLeft" GUITexture button is pressed   
                if(myLeftHitTest) 
               {  
               	Player.AddForce (-Speed * Time.deltaTime,0, 0);     
                  
               }
               
               // Make Player move right if the "ArrowRight" GUITexture button is pressed   
                if(myRightHitTest) 
               {  
               	Player.AddForce (Speed * Time.deltaTime,0, 0);     
                  
               }    
                
         } 
} 

}

If anyone has any suggestions on how to improve upon this JavaScript please feel free to add to it.

Since this code works, were you able to get it to work in the editor with the mouse, and if so, how?