Raycast not working on touchscreen (Android tablet/phone)

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.

My issue seems to be that the rectTransfrom positionof objects inside the canvas aren't changing. i.e., at a 16:9, something at the top of the screen is fine, but in a 4:3 it remains at the top and gets cut off due to the height being smaller. It seems like an anchoring problem.

2 Answers

2

You are trying to compile code on an Android device with input.getmouseposition you need to use the input.gettouches. all of the mouse and mouse button commands won’t work if you don’t have a mouse

And scale with screen size isn't taking care of it? Do you have any code controlling their size? Maybe a component setting was accidentally checked? It's always the little things that get you, if you've been at it for a while take a short break and take your mind off it ^^

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.