Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a Touch point is inside box collider in Unity

Please see the below images.

enter image description here

enter image description here

In the first image you can see that there is box collider. The second image is when I run the code on Android device

Here is the code which is attached to Play Game (its a 3D Text)

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {   

    public string levelToLoad;
    public AudioClip soundhover ;
    public AudioClip beep;
    public bool QuitButton;
    public Transform mButton;
    BoxCollider boxCollider;

    void Start () {
        boxCollider = mButton.collider as  BoxCollider;
    }

    void Update () {

        foreach (Touch touch in Input.touches) {

            if (touch.phase == TouchPhase.Began) {

                if (boxCollider.bounds.Contains (touch.position)) {
                    Application.LoadLevel (levelToLoad);
                }
            }                
        }
    }
}

I want to see if touch point is inside the collider or not. I want to do this because right now if I click any where on the scene Application.LoadLevel(levelToLoad); is called.

I want it to be called if I click on PLAY GAME text only. Can any one help me with this piece of code or can give me another solution to my problem??


Recent Code by following Heisenbug's Logic

void Update () {

foreach( Touch touch in Input.touches ) {

    if( touch.phase == TouchPhase.Began ) {

        Ray ray = camera.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, 10)) {
            Application.LoadLevel(levelToLoad);             
        }           
    }   
}
}
like image 614
Jawad Amjad Avatar asked May 08 '13 14:05

Jawad Amjad


1 Answers

Touch's position is expressed in screen space coordinate system (a Vector2). You need to convert that position in the world space coordinate system before trying to compare it against other 3D locations of objects in the scene.

Unity3D provides facility to do that. Since you are using a BoundingBox surrounding your text, you can do the following:

  • Create a Ray which origin is in the touch point position and which direction is parallel to the camera forward axis (Camera.ScreenPointToRay).
  • Check if that ray intersect the BoundingBox of your GameObject (Physic.RayCast).

the code may look something like that:

Ray ray = camera.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerOfYourGameObject))
{
   //enter here if the object has been hit. The first hit object belongin to the layer "layerOfYourGameObject" is returned.
}

It's convenient to add a specific layer to your "Play Game" GameObject, in order to make the ray colliding only with it.


EDIT

Code and explanation above is fine. If you don't get a proper collision maybe you have not used the right layer. I haven't a touch device at the moment. The following code will work with mouse (without using layers).

using UnityEngine;
using System.Collections;

public class TestRay : MonoBehaviour {

    void Update () {

        if (Input.GetMouseButton(0))
        {
            Vector3 pos = Input.mousePosition;
            Debug.Log("Mouse pressed " + pos);

            Ray ray = Camera.mainCamera.ScreenPointToRay(pos);
            if(Physics.Raycast(ray))
            {
                Debug.Log("Something hit");
            }

        }
    }

}

It's just an example to put you in the right direction. Try to figure out what's going wrong in your case or post an SSCCE.

like image 101
Heisenbug Avatar answered Oct 08 '22 22:10

Heisenbug