Log in or register to post comments

Touch object with Unity

January 17, 2013 - 12:30am #1

 

Hi,

This is my first post. I am using android settings for my project & im more familiar with JavaScript than C#.

I have a problem with the touch functionality. I hv added sphere & cube. Then i added a script where if i touch the sphere, the cube will dissapear using the render enable = false. When i deploy to my android n tried to touch the sphere on the screen, the cube dissapear n it works pretty well.

The problem is that when im using vuforia SDK, it did not work. When i touch the sphere on the screen nothing happens. I did drag the script to the sphere object but still it wont work. I really need to solve this problem. any ideas anyone?.. ur feedback is highly appreciated.

 

Below is my JavaScript:-

 function Update () { 

 
      for(var i:int = 0; i < Input.touches.Length; i++)
    { 
        var touch:Touch = Input.touches[i];
        var ray:Ray = Camera.main.ScreenPointToRay(touch.position); 
        var hit:RaycastHit = new RaycastHit(); 
 
        if(Physics.Raycast(ray,hit, 1000)) 
        { 
            if(hit.collider.gameObject == this.gameObject) 
            { 
                switch(touch.phase) 
                { 
                    case TouchPhase.Began:
                         //the cube should be gone when i touch
                        gameObject.Find("Cube").renderer.enabled = false;
                    break;
 
                    default: 
                    break; 
                } 
            } 
        }   
      } 
}  
 

You're welcome.

January 17, 2013 - 2:22am #4

You're welcome.

Thanks a lot AlessandroB for

January 17, 2013 - 2:14am #3

Thanks a lot AlessandroB for solution.

It works pretty well now. I've added the collision detection n it works!

really appreciate the help :)

Hi, does your sphere has a

January 17, 2013 - 1:40am #2

Hi, does your sphere has a sphere collider component ?

if yes, then your script should detect the collision; I just made the following test:

- attaching the javascript to the ARCamera

- modifying your code as follow (just to verify that raycast is working):

 

    for(var i:int = 0; i < Input.touches.Length; i++)
    { 
        Debug.Log("Touch ");
        
        var touch:Touch = Input.touches[i];
        
        var ray:Ray = Camera.main.ScreenPointToRay(touch.position); 
        var hit:RaycastHit = new RaycastHit(); 
 
        if( Physics.Raycast(ray, hit, 10000)) 
        {
        Debug.Log("Hitting something: " + hit.collider.gameObject.name);
       
        }
    }
 
In my test I just had to add a Mesh Collider to the Teapot objects in the sample, and it did work.
 
If you test this code and check the Logs, you should see that the Hitting something: message is displayed; if not, it means there is something wrong with the collision detection (e.g. the collider is missing or has some wrong settings)
 
If you verify that the code above works, then you can narrow down the problem.
Log in or register to post comments