- Home /
get/return the touched object name when i touch on the screen
I am doing a map, there are many objects in the map, what i want to realize is when one finger touch on the screen,i want to know which object i touch,there may be a function can return the object name. i am thinking about the "raycast" function, however it doesn't work. can anybody help me?
Can you show us how you are using the raycast? It should work.
Touch touch = Input.touches[0];
Ray touchRay = cam.ScreenPointToRay(touch.position);
foreach( RaycastHit hit in Physics.RaycastAll(touchRay) ) {
print("touching object name="+hit.transform.name);
}
here is partial code. thanks for your help.
What kind of problem do you have ? Does it log something in console ?
Anyway, try that code :
void Update()
{
if ( Input.touchCount != 0 )
{
Touch touch = Input.touches[0];
Ray ray = Camera.mainCamera.ScreenPointToRay(touch.position);
RaycastHit hit;
if ( Physics.Raycast(ray, out hit, 100f ) )
{
Debug.Log(hit.transform.gameObject.name);
}
}
}
in fact, i write the code "print(hit.transform.gameObject.name);"and nothing print out.
$$anonymous$$y project is a map using gesture work. i put these code in Drag function, i want to realize when my finger on the screen,there is a function can return the name of object which i am touching on. Do you have any good idea?
I didn't know there was a print(). But I guess if you've been using it so far, it must work well. :)
Looking at your code, it's quite possible that your Raycast does not hit anything and thus foreach statement is never entered. Try to specify a distance in your RaycastAll function (though that shouldn't be it, since default is infinity) :
Physics.RaycastAll ( ray, 100f );
BTW, you should also check that your objects are not on the IgnoreRaycast layer. Also, try to draw your ray like this and see if it hit anything in your scene :
Debug.DrawLine( ray.origin, ray.origin+ray.direction*100f );
Answer by tapticc · Mar 08, 2014 at 07:48 PM
For other noobs like me, you need to add a collider e.g. a box collider to the object you want to touch. I feel like such a noob although I am only 3 weeks in to Unity lol
Answer by amphoterik · Jun 25, 2013 at 12:57 PM
I have modified your code a bit. Try this:
Touch touch = Input.touches[0];
Ray touchRay = Camera.mainCamera..ScreenPointToRay(touch.position);
RaycastHit[] hits = Physics.RaycastAll(touchRay);
foreach( RaycastHit hit in hits ) {
print("touching object name="+hit.gameObject.name);
}
Minor changes. See if they work.
EDIT: Do you need all of the objects hit or just the first object? RaycastAll may not be doing what you want. You may want to consider (as was posted already) using:
Physics.Raycast(ray, out hit);
Instead
Answer by Imankit · Jun 25, 2013 at 12:51 PM
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Input.GetMouseButton(0)) {
if(Physics.Raycast(ray, out hit)) {
print(hit.collider.name);
}
}
This is for C#
Imankit, this is piece of code for mouse click, not for touch
Your answer

Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
How do I check if an object is touching another object? 1 Answer
What's the matter with this script? Detect object dragging with iPhone Touch 1 Answer
touch appear let go dissapear? 2 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers