4

I'm developing a game on Unity for iOS devices. I've implemented the following code for touch:

void Update () {
    ApplyForce ();
}

void ApplyForce() {

    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
        Debug.Log("Touch Occured");     
    }
}

I've dragged this script onto my game object which is a sphere. But the log message appears no matter where I touch. I want to detect touch only when the users touches the object.

Bart
  • 19,692
  • 7
  • 68
  • 77
Ashish Beuwria
  • 975
  • 6
  • 13
  • 17
  • for this very old QA .. modern Unity: https://stackoverflow.com/questions/40323677/using-unity3ds-ipointerdownhandler-approach-but-with-the-whole-screen – Fattie Jan 23 '19 at 16:40

2 Answers2

18

stick the code bellow in your camera, or something else that will persist in the level you're designing. It can be anything, even an empty gameobject. The only important thing is that it only exists ONCE in your level, otherwise you'll have multiple touch checks running at the same time, which will cause some seriously heavy load on the system.

void Update () {
    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) 
    {
        Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
        RaycastHit hit;

        if ( Physics.Raycast(ray, out hit) && hit.transform.gameObject.Name == "myGameObjectName")
        {
            hit.GetComponent<TouchObjectScript>().ApplyForce();  
        }
    }
}

In the above script, hit.transform.gameObject.Name == "myGameObjectName" can be replaced by any other method you want to check that the object hit by the raycast is the object you want to apply a force to. One easy method is by Tags for example.

Another thing to keep in mind is that raycast will originate at your camera (at the position relative to your finger's touch) and hit the first object with a collider. So if your object is hidden behind something else, you won't be able to touch it.

Also put the script bellow in your touch object (in this example, called TouchObjectScript.cs)

void Update () {
}

public void ApplyForce() {

        Debug.Log("Touch Occured");   
}

If something wasn't clear, or you need any further help, leave a comment and I'll get back to you.

Steven Mills
  • 2,363
  • 26
  • 36
  • 2
    Dangit ... needed just a minute to post the same. So yeah, +1. ;) – Bart Jan 28 '14 at 15:55
  • @Steven Is there any problem if I attach this script to my sphere or the game object? What about using `collider.Raycast` instead of `Physics.Raycast`? and what is screenPos? I've used `Input.GetTouch (0).position` in place of it. – Ashish Beuwria Jan 28 '14 at 16:05
  • @Steven I attached it to the camera and it worked. Now I got the point why the script needs to be attached to the camera instead of the object itself, as the ray is casted from the camera to hit the object. – Ashish Beuwria Jan 28 '14 at 16:37
  • But, you can still tell the answer to other two questions. – Ashish Beuwria Jan 28 '14 at 16:38
  • 1
    error CS1061: Type `UnityEngine.RaycastHit' does not contain a definition for `gameObject' and no extension method `gameObject' of type `UnityEngine.RaycastHit' could be found (are you missing a using directive or an assembly reference?) – Ahmed Mohsen May 08 '15 at 07:05
  • 1
    @AhmedMohsen don't know how that went unnoticed for so long... I've update my answer, thanks – Steven Mills May 08 '15 at 07:14
2

The best way to do this, is just adding:

void OnMouseDown () {}

to the gameobject you want to be clicked. The problem with this is that you cannot press 2 things at one time.