Changing from mouse input to touchscreen

I’m working on converting one of my games from using the mouse input to using a touchscreen instead, but I have no experience with the touchscreen scripting commands. I’m trying to figure out how to approximate the same thing I was doing with the mouse:

There’s an 8x8 grid of tiles, each with a collider and a script with an OnMouseOver() function. When the mouse is over the object, it will watch for the mouse button status and register one of several things:

  • Clicking and holding the left mouse button starts drawing a path (marking the tile as the first tile in the path)
  • Dragging the left mouse button over another adjacent tile continues the path onto that tile
  • Letting go of the left mouse button completes the path

Since the touch controls don’t work the same way, I need to know the best way to approximate the same thing. I already know that I should break all of the path functions out of the OnMouseOver() function and into separate functions (so that I can trigger them separately from the mouse and touch controls). What I don’t know is how to use the touch controls to:

  • Register which tile was touched
  • Register which tile the finger was slid onto
  • Register which tile the finger was over when it is lifted

Anyone have suggestions on how I can proceed with this?

You can use Input.touch

if(Input.touchCount > 0) {
            Touch touch = Input.touches[0]; // First finger touch
           
            if(touch.phase == TouchPhase.Began) {
                Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0.0f));
                RaycastHit hit;
               
                if(Physics.Raycast(ray, out hit)) {
                    // Start your tile, use LayerMask or do something to filter out unwanted game objects
                }
            }
           
            if(touch.phase == TouchPhase.Moved) {
                Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0.0f));
                RaycastHit hit;
               
                if(Physics.Raycast(ray, out hit)) {
                    // Continue checking for new tiles if you use a List<T> you can check if object already exits, if it doenst add it, use LayerMask or do something to filter out unwanted game objects
                }
            }
           
            if(touch.phase == TouchPhase.Ended) {
                Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0.0f));
                RaycastHit hit;
               
                if(Physics.Raycast(ray, out hit)) {
                    // End your tile, use LayerMask or do something to filter out unwanted game objects
                }
            }
        }

Thanks, I’ll give this a try. :slight_smile: