Log in or register to post comments

Detecting touch on object

December 19, 2012 - 11:28am #1

I know this is an oft-asked query and I have searched and tried several methods to no avail. Hoping someone can help me out here. Following the code somewhere on this forum, this is what I've done:

Created OnTouchDown.cs with the following code and attached it to the ARCamera prefab:

</p><p>using UnityEngine;<br />using System.Collections;<br />using System.Collections.Generic;<br /><br />public class OnTouchDown : MonoBehaviour<br />{<br />    void Update () {<br />        RaycastHit hit = new RaycastHit();<br />        for (int i = 0; i < Input.touchCount; ++i) {<br />            if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {<br />            // Construct a ray from the current touch coordinates<br />            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);<br />            if (Physics.Raycast(ray, out hit)) {<br />                hit.transform.gameObject.SendMessage("OnMouseDown");<br />              }<br />           }<br />       }<br />    }<br />}</p><p>

 

Created ObjectHighlighter.cs and added to the objects in the scene:

 

</p><p>using UnityEngine;<br />using System.Collections;<br />public class ObjectHighlighter : MonoBehaviour {<br />    bool isHighlighted = false;<br />    Material originalMaterial;<br />    Material redMaterial;<br />    MeshRenderer meshRenderer;<br />    GameObject baseObject;<br />    string obj_name;<br />    // Use this for initialization<br />    void Start () {<br />        obj_name             = this.gameObject.name + "Base";<br />        baseObject             = GameObject.Find( obj_name );<br />        meshRenderer         = baseObject.GetComponent<MeshRenderer>();<br />        originalMaterial     = meshRenderer.material;<br />        Color red             = new Color(255.0f,0.0f,0.0f, 0.5f);<br />        redMaterial          = new Material(Shader.Find("Transparent/Parallax Specular"));<br />        redMaterial.color     = red;<br />    }<br />    // Update is called once per frame<br />    void Update () {<br />    }<br />    void OnMouseDown(){<br />        Debug.Log("OMD "+obj_name);<br />        isHighlighted = !isHighlighted;<br />        if( isHighlighted ){<br />            HighlightRed();<br />        }else{<br />            RemoveHighlight();<br />        }<br />    }<br />    void HighlightRed(){<br />        meshRenderer.material = redMaterial;<br />    }<br />    void RemoveHighlight(){<br />        meshRenderer.material = originalMaterial;<br />    }<br />}</p><p>

The objects have colliders on them. The AR part works fine but the touch is not being detected at all.

 

I'm testing on an Android device, if it helps.

 

Thanks!

Detecting touch on object

March 20, 2017 - 4:12am #6

Thank you!

Awesome, glad I could help :)

December 20, 2012 - 10:00am #5

Awesome, glad I could help :)

Thank you NalinS, that worked

December 20, 2012 - 9:38am #4

Thank you NalinS, that worked great!

Hi hsn22 Can you try this and

December 20, 2012 - 3:12am #3

Hi hsn22

Can you try this and see if it works by attaching it to an empty game object in the scene.

It is a bit simpler so it may help you progress a little.

HTH

N

 

 

 

void Update () {

 

if (Input.GetMouseButtonDown (0))

{

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

ShootRay(ray);

}

}

 

void ShootRay (Ray ray)

{  

 

RaycastHit rhit;

 

bool objectHit = false;

GameObject gObjectHit = null;

 

 

if (Physics.Raycast (ray, out rhit, 1000.0f)) {

  Debug.Log("Ray Shot and hit!");

  objectHit = true;

  gObjectHit = rhit.collider.gameObject;

}

 

}

 

 

Have you tried logging (

December 19, 2012 - 5:07pm #2

Have you tried logging ( Debug.Log( ... ) ) from Update or the highlighter script to determine what, if anything, is being hit?

Log in or register to post comments