android - Checking if a Touch point is inside box collider in Unity -


please see below images.

enter image description here

enter image description here

in first image can see there box collider. second image when run code on android device

here code attached play game (its 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  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 see if touch point inside collider or not. want because right if click on scene application.loadlevel(leveltoload); called.

i want called if click on play game text only. can 1 me piece of code or can give me solution problem??


recent code 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);                      }                }    } } 

touch's position expressed in screen space coordinate system (a vector2). need convert position in world space coordinate system before trying compare against other 3d locations of objects in scene.

unity3d provides facility that. since using boundingbox surrounding text, can following:

the code may 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 object has been hit. first hit object belongin layer "layerofyourgameobject" returned. } 

it's convenient add specific layer "play game" gameobject, in order make ray colliding it.


edit

code , explanation above fine. if don't proper collision maybe have not used right layer. haven't touch device @ moment. following code work 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 example put in right direction. try figure out what's going wrong in case or post sscce.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -