Scripting a "touch" on an object in iOS.

I’m sure this is a simple fix to most of you but I have been racking my brains out trying to figure it out.

I am working on a first person game and I want to be able to change scenes when the player comes to a door (cube) and touches it with his finger. I have been working on this script:

function Update (){

   if(Input.GetMouseButtonUp(0))
   {
           Debug.Log("I touched the wall!"); //I will be changing the Debug.Log to a
                                         // scene change when it is working properly.
   }

}

but all I seem to get is a printout in the console every time the player’s finger is taken off the screen… no matter what he touches. I would like it to happen only when the player touches a particular cube that looks like a door.

I would really appreciate any help that anyone may be able to supply. Thanks so much.

Tom

The problem is in IOS you don’t have a Mouse, you have to use input.GetTouch().
Then you have to get the position of that touch with input.GetTouch().position.
Then you have to Cast a Ray from the User’s perspective (the camera) to that position in the World (using Camera.SreenPointToRay() )

And then test if the Ray hits the Cube (door).

If that happens then you change the scene

Try this:

//A Vector to store the touch position
Vector2 userClic;

//If the user has touched anywhere...
if(Input.touchCount > 0)  
    //Then, store the touch position
    userClic = Input.GetTouch(0).position;

//Create a ray base on the camera's position and the userClic
Ray userRay =  Camera.mainCamera.ScreenPointToRay(userClic);

//an object to store the ray cast info
RaycastHit info;

//If the ray hits any object...
if( Physics.Raycast(userRay,out info, 100))
{
    //If that object is a Door (You have to label the object "Door")
    if(info.collider.tag == "Door")
    {
     //Here you open your door i.e. change the scene...
    }
}

Hope it helps…

Don't omit the RayCast part, it's important. A ray is a mathematical (invisible) line. In this case this ray is calculated based on the touch's position and the camera's projection using the function ScreenPointToRay. This function "projects" the point (in the device's 2D screen) to a Line in the world, starting from the Camera's position and until it reaches 100 units "into" the device.

function Update () {
for (var touch : Touch in Input.touches) {
    if (touch.phase == TouchPhase.Began) {
       //Here you have the touch's position so, This is where you have to create a ray using ScreePointToRay
       var userRay = Camera.main.ScreenPointToRay(touch.position) ;
//This is to store the RayCasting results
       var info : RaycastHit ;
//[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something
    if(Physics.Raycast(userRay, info, 100.0f))
    {
      //This tells us if the thing it hit was a "Door".
      if(info.transform.CompareTag("Door"))
      {
            // This is where you have to put the change level info
           Debug.Log ("I touched the door!");
           Application.LoadLevel ("Level2");

      }

     }
  }
}

}

It looks awful and tedious but that's what you have to do to hit something. Also, this looks terrible becasue the comments, but you can do it cleaner if you prefer. But please do not take away the Raycast part.

var hit: RaycastHit;
var ray;

function Update () 
{
    if(Input.GetMouseButtonDown(0)) 
       {
         if (Physics.Raycast (ray, hit, 100)) 
           {
              if(hit.collider.gameObject.tag == "Door")  // tag your cube with the name Door
                {
                 // write in your code here 
              }
       }
}