0
\$\begingroup\$

I have a raycast that works in the unity editor but not when I build it for Android( tablet or phone, neither work). The code I'm using is as follows:

public class DirectionalTilePlacing : MonoBehaviour {

public LayerMask levelLayer;
private int tileType;
public int Tiletype
    {
        get { return tileType; }
    set
    {
        tileType = value;
    }
    }
RaycastHit hit;
Vector2I placePosition;
// Update is called once per frame
void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit, Mathf.Infinity, levelLayer))
    {
        if (hit.collider.tag == "Replaceable")
        {

            transform.position = new Vector3(Mathf.Floor(hit.point.x) + 0.5f, Mathf.Floor(hit.point.y), Mathf.Floor(hit.point.z) + 0.5f);
            GetComponent<MeshRenderer>().enabled = true;
        }
    }
    if (Input.GetMouseButtonUp(0))
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, levelLayer))
            if (hit.collider.tag == "Replaceable")
            {
                placePosition = new Vector2I((int)transform.position.x, (int)transform.position.z);
                EditorManager.map.SetEmpty(placePosition);
                EditorManager.map.BrushTile(EditorManager.map.TypeToObject((Tile.tileType)tileType), placePosition, (Tile.tileType)tileType);
                GetComponent<MeshRenderer>().enabled = false;
                gameObject.SetActive(false);
            }
    }

}

}

Everything up until any Physics.Raycast line works. The idea is that there is a ray casted from the position your finger is at when touching the screen.

\$\endgroup\$
0
\$\begingroup\$

Instead of using Input.getMouseButton try this

if you want to detect only one screen touch use this:

 if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit, Mathf.Infinity, levelLayer))
        if (hit.collider.tag == "Replaceable")
        {
            placePosition = new Vector2I((int)transform.position.x, (int)transform.position.z);
            EditorManager.map.SetEmpty(placePosition);
            EditorManager.map.BrushTile(EditorManager.map.TypeToObject((Tile.tileType)tileType), placePosition, (Tile.tileType)tileType);
            GetComponent<MeshRenderer>().enabled = false;
            gameObject.SetActive(false);
        }
}

but if you want to detect all touches use this

for (int i = 0; i < Input.touchCount; ++i) {

    if (Input.GetTouch(i).phase == TouchPhase.Ended){
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit, Mathf.Infinity, levelLayer))
        if (hit.collider.tag == "Replaceable")
        {
            placePosition = new Vector2I((int)transform.position.x, (int)transform.position.z);
            EditorManager.map.SetEmpty(placePosition);
            EditorManager.map.BrushTile(EditorManager.map.TypeToObject((Tile.tileType)tileType), placePosition, (Tile.tileType)tileType);
            GetComponent<MeshRenderer>().enabled = false;
            gameObject.SetActive(false);
        }

    }
}
\$\endgroup\$
  • \$\begingroup\$ This works the same way mouse input works. Because when you build it for Android Unity converts mouse input to touch input. \$\endgroup\$ – vonis22 Aug 1 '16 at 7:38
0
\$\begingroup\$

The answer was pretty simple. I found out that none of the objects in question had a collider whatsoever. So added colliders and everything works now. Thanks anyhow.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.