0

Why this code does not print anything after a touch?

    void Update()
{
    if (Input.touchCount > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
        if (hit)
        {
            print(hit.transform.name);
        }
    }
}
Sharpy
  • 143
  • 2
  • 8
  • Because it wants you to give us more information. E.g. do the objects you try to hit with the raycast have a collider2d of some sort, might they be set to ignore raycasts? – Gunnar B. Jan 15 '17 at 14:07
  • What type of Object are you trying to hit? Putting screenshot of it while it is selected would be helpful. Also, put `print("inside touch");` in `if (Input.touchCount > 0)`. You have to tell us what is failing in the code. – Programmer Jan 15 '17 at 14:08
  • Also shouldn't you be looking for the name of the gameobject the transform is attached to? So something like `hit.transform.gameObject.name`? – UnholySheep Jan 15 '17 at 14:09
  • @Programmer why this Camera.main.ScreenToWorldPoint(Input.touches[0].position) returns the Camera position instead of touch position? – Sharpy Jan 15 '17 at 14:25
  • Your last question/comment is false. Please consider answering my question my first comment. Don't ignore other comments too since their comments means that they want to help you. – Programmer Jan 15 '17 at 14:37
  • @Programmer ok, sorry. It's an empy 2d gameObject with a box collider. Screenshot: http://screenshot.ru/4efa2bc6302e857af87d5c5555dad8f5.png – Sharpy Jan 15 '17 at 14:43
  • @GunnarB. I found that Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) returns a camera position. Does it should work like that? – Sharpy Jan 15 '17 at 14:46
  • @UnholySheep idk it returned the name with this `hit.transform.name` when I used Input.GetTouch(0).deltaPosition instead of Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) – Sharpy Jan 15 '17 at 14:48
  • Hi, the code in your question should be working. What are you trying to do ?What is the exact problem you are having? Please answer these two questions – Programmer Jan 15 '17 at 14:56
  • @Programmer Oh, I had to used this `Vector3 V2toV3 = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 10f);` because ScreenToWorldPoint receives a Vector3 argument where x and y are the screen coordinates, and z is the distance from the camera. Since Input.GetTouch(0).position.z is always 0, what I was getting is the camera position. Now it's working – Sharpy Jan 15 '17 at 14:59
  • I've been using that code and it should work. The code in your [last](http://stackoverflow.com/questions/41659982/raycast-returns-null) question is even working. I can't tell why this is not working for you. Maybe you are using old Unity version? Glad you fixed your own problem. – Programmer Jan 15 '17 at 15:04
  • I suspect that there should be another collider (raycast starts in or near it) and since raycast only hits one target then ray never get past that – Bizhan Jan 15 '17 at 15:46

1 Answers1

-1

Try this:

if (Input.touchCount > 0)
    {
        RaycastHit2D hit;

        if (Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero, out hit))
        {
            print(hit.transform.name);
        }
    }
Sergio
  • 43
  • 10