Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JaamiParvez · Jul 20, 2014 at 01:01 PM · mousetouchconvert

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;
 
         }
     }
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 20, 2014 at 03:09 PM 0
Share

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.

avatar image JaamiParvez · Jul 21, 2014 at 03:29 PM 0
Share

On$$anonymous$$ouse* functions works on andriod but not responsive.. is there a better way to make touch more responsive??

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
             
             
         }
         
     }
     
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges