- Home /
Convert Mouse-Input to Touch-Input..How??
This is the script for mouse input. Can someone please tell how to convert this for Andriod Touch Input???
using UnityEngine;
using System.Collections;
public class MouseDrag : MonoBehaviour
{
public float speed = 1f;
private float baseAngle = 0.0f;
private float maxRotaion = 80f;
private Quaternion currentRotation,defaultRotation;
void Start()
{
defaultRotation = transform.rotation;
}
void OnMouseDown ()
{
var dir = Camera.main.WorldToScreenPoint (transform.position);
dir = Input.mousePosition - dir;
baseAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
}
void OnMouseDrag ()
{
// Limit platform rotation
var dir = Camera.main.WorldToScreenPoint (transform.position);
dir = Input.mousePosition - dir;
var angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - baseAngle;
if (angle >= -maxRotaion && angle <= maxRotaion)
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
currentRotation = transform.rotation;
}
void OnMouseUp()
{
StartCoroutine (RotateObject (transform, currentRotation, defaultRotation, 3.0f));
}
IEnumerator RotateObject(Transform thisTransform, Quaternion from, Quaternion to, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate * speed;
transform.rotation = Quaternion.Slerp(from, to, i);
yield return null;
}
}
}
The On$$anonymous$$ouse* functions should work unchanged on Android.
Can someone please convert this for Andriod Touch Input???
Unity Answers does not do work for you. We give you the information to do your own work.
On$$anonymous$$ouse* functions works on andriod but not responsive.. is there a better way to make touch more responsive??
Answer by citizen_rafiq · Jul 20, 2014 at 03:25 PM
I didn't understand what are you trying to say about conversion. for single touch event I always use bellow approach
void Update () {
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
_down_pos.x = touch.position.x;
_down_pos.y = touch.position.y;
_direction = Vector2.zero;
RaycastHit2D hit = CastARay (touch.position);
if (hit != null)
{
if (hit.collider !=null)
{
OnDownGUIWidget(hit);
}
}
}
if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
_up_pos.x = touch.position.x;
_up_pos.y = touch.position.y;
}
}
}
}
else
{
if (Input.GetMouseButtonDown(0))
{
_mouse_down = true;
_direction = Vector2.zero;
_down_pos.x = Input.mousePosition.x;
_down_pos.y = Input.mousePosition.y;
RaycastHit2D hit = CastARay (Input.mousePosition);
if (hit != null)
{
if (hit.collider !=null)
{
OnDownGUIWidget(hit);
}
}
}
if (Input.GetMouseButtonUp(0))
{
_up_pos.x = Input.mousePosition.x;
_up_pos.y = Input.mousePosition.y;
_mouse_down = false;
OnClickGUIWidget(Input.mousePosition);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613154537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to test Touch event without IOS? 1 Answer
Convert Touch input to Mouse input C#? 1 Answer
Changing mouse input to touch input... How ? 0 Answers
Convert Mouse Input to Touch Input 0 Answers
Touch / mouse code. 1 Answer