I am trying to develop an augmented reality iOS app in Unity with Vuforia SDK. I am struggling with trying to make my GameObjects touchable.
Currently, I am able to get my GameObjects to hover over my markers as expected. However, when tapping on them, nothing happens.
Here is my hierarchy.
Now, I've been browsing forums and online tutorials for how to do this and here is the code I have so far.
I have two C# scripts: touchableManager (attached to ARCamera) and touchableGameobject (attached to Cube and Cube)
touchableManager:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class touchableManager : MonoBehaviour
{
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
void Update ()
{
if (Input.touchCount > 0)
{
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches)
{
Ray ray = GetComponent<Camera>().ScreenPointToRay (touch.position); //attached the main camera
if (Physics.Raycast(ray, out hit, 100f, touchInputMask.value))
{
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if (touch.phase == TouchPhase.Began) {
recipient.SendMessage ("onTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Ended) {
recipient.SendMessage ("onTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
recipient.SendMessage ("onTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Canceled) {
recipient.SendMessage ("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld)
{
if (!touchList.Contains(g))
{
g.SendMessage("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
touchableGameobject:
using UnityEngine;
using System.Collections;
public class touchableGameobject : MonoBehaviour
{
public Color defaultColor;
public Color selectedColor;
private Material mat;
void Start()
{
mat = GetComponent<Renderer>().material;
}
void onTouchDown()
{
mat.color = selectedColor;
}
void onTouchUp()
{
mat.color = defaultColor;
}
void onTouchStay()
{
mat.color = selectedColor;
}
void onTouchExit()
{
mat.color = defaultColor;
}
}
So far, all my application does is reveal the two GameObjects. When I tap them, nothing happens. The code should change the color of the cubes when tapped.
Please, any help would be greatly appreciated. Please guide me along the right path.
Edit: Box Colliders are added. See screenshot.