Convert Mouse Input to Touch Input

Hi guys. I have done a games for PC, but my target platform is Android/iOS. So the problem is how to convert Mouse Input to Touch Input. Below is my code. Can anyone help me? thanks in advance !

using UnityEngine;
 
using System.Collections;
 
public class DragDrop : MonoBehaviour
{
    private Vector3 screenPoint;
    private Vector3 offset;
 
 
    void OnMouseDown() 
    { 
       screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
 
       offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 
       Screen.showCursor = false;
 
 
    }
 
    void OnMouseDrag() 
    { 
       Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
       Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 
       transform.position = curPosition;
 
    }
    void OnMouseUp()
       {
         Screen.showCursor=true;
 
    }
    }

In general terms, if I was building for a touch environment, I would build it for both at the same time. I don’t use OnMouseDown or things like that, I do them all through the Update function and use Input.GetMouseButtonDown.

void Update(){
	if (Input.touches.GetLength () > 0) {
		// handle Touches
	} else {
		// handle mouse input.
	}
}

Using this format, I would then develop methods that do things. So, if you touch start and your touch is in the right place, then you call, CubeTouched, or if you mouseButtonDown and the mouse position is in the right place, you call CubeTouched. Both ways then call the same code.

@bigmisterb sorry, i’ve learned Unity for few months, so I stil don’t understand what you said clearly. So can you help me to convert my code so that I can use for my games?

Fun facts:
If you use Input.GetButton or its variants, with Fire1 as an argument under the default controls, it automatically translates that to mean mouse on a desktop platform, and touch on mobile. Fire2 would represent a second touch on the screen while the first touch is held.

Input.mousePosition will return the position a touch is held, or the spot most recently touched.

With those two bits of knowledge, it’s very easy to build an input system that is cross-platform without any special efforts. But you do have to detect your input in update.

you mean that I just need to replace OnMouseDown,OnMouseDrag,OnMouseUp by Input.GetButton , so the code will work?

he means like this:

	void Update(){
		if (Input.GetButtonDown ("Fire 1")) {
			screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
			offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
			Screen.showCursor = false;
		}

		if (Input.GetButton ("Fire 1")) {
			Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
			Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
			transform.position = curPosition;
		}

		if (Input.GetButtonUp ("Fire 1")) {
			Screen.showCursor=true;
		}
	}
2 Likes

thank you very much @bigmisterb :smile:

Please Help.

Ned this Mouse input drag script to run on Touch on Android and dont know how to make this Cs. To become from mouse input to touch four android.

Basicly need simple drag and drop four android to drag simple 3d Cube.
Tnx 2 all.

public class DragBody : MonoBehaviour
{
//Initialize Variables
GameObject getTarget;
bool isMouseDragging;
Vector3 offsetValue;
Vector3 positionOfScreen;

// Use this for initialization
void Start()
{

}

void Update()
{

//Mouse Button Press Down
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget != null)
{
isMouseDragging = true;
//Converting world position to screen position.
positionOfScreen = Camera.main.WorldToScreenPoint(getTarget.transform.position);
offsetValue = getTarget.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z));
}
}

//Mouse Button Up
if (Input.GetMouseButtonUp(0))
{
isMouseDragging = false;
}

//Is mouse Moving
if (isMouseDragging)
{
//tracking mouse position.
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionOfScreen.z);

//converting screen position to world position with offset changes.
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offsetValue;

//It will update target gameobject’s current postion.
getTarget.transform.position = currentPosition;
}

}

//Method to Return Clicked Object
GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject target = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
target = hit.collider.gameObject;
}
return target;
}

}