- Home /
How to convert mouse input to mobile touch in unity
I am new to game development. I am try to make something like rise up. I tried to change the flowing code to make workable for mobile touchscreen unfortunately failed. can you help me how can I make flowing code for mobile?
private Vector2 mousePos;
private Rigidbody2D rb;
private Vector2 offsetClicked;
private Vector2 offsetReleased;
private void Start () {
rb = GetComponent<Rigidbody2D> ();
offsetReleased = transform.position;
}
private void FixedUpdate () {
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (Input.GetMouseButton (0)) {
Vector2 newPos = new Vector2 (
Mathf.Clamp(mousePos.x + offsetClicked.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
mousePos.y + offsetClicked.y
);
rb.MovePosition (newPos);
offsetReleased = newPos - (Vector2) Camera.main.transform.position;
} //Clicked
else {
Vector2 newPos = new Vector2 (
Mathf.Clamp (Camera.main.transform.position.x + offsetReleased.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
Camera.main.transform.position.y + offsetReleased.y
);
rb.MovePosition (newPos);
offsetClicked = newPos - mousePos;
} //Released
}
Above code working for mouse. Thanks you
Answer by MSavioti · Sep 07, 2018 at 06:14 PM
The Unity has its own touch library:
https://docs.unity3d.com/ScriptReference/Touch.html
As far as I know, you're going have to change all your script manually. I'm yet to do this in a quiz game for android, thanks to my laziness.
would help me please to change all script. I am spend almost 2 days but failed to mange this things
Your answer
Follow this Question
Related Questions
How to make Mobile Touch Controls work when pressing the left and right side of the screen 1 Answer
Make an obect take priority over another when touched? 0 Answers
Inconsistent Mouse Related Behavior in 2D 1 Answer
Touch input broken with multi-touch 1 Answer
How can I convert a OnMouseDown() command to a GetTouch input properly? 0 Answers