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 Basen · May 02, 2015 at 10:25 AM · c#unity5touchdrag

Unity Touch Controls, Drag and DoubleTap

Greetings,

I've been bashing my head over this one for the past two days, and I just can't get it to work... I've been using the mouse controls for single touch controls on mobile devices. However I've noticed some hiccups here and there and would rather use the touch controls. I've checked the entire web for a solution but none worked for me for some reason... I've pasted the code I have atm for mouse, I need help converting that for touch controls.

THANK YOU in advance!

     void OnMouseDown()
         {
         distance = Vector3.Distance (transform.position,  Camera.main.transform.position);
    dragging = true;
             }
 void OnMouseUp()
     {
         dragging = false;
         if ((Time.time - doubleClickStart) < 0.3f)
         {
             this.OnDoubleClick();
             doubleClickStart = -1;
         }
         else
         {
             doubleClickStart = Time.time;
         }
 
 void Update()
     {    if (dragging) 
             {
                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 Vector3 rayPoint = ray.GetPoint (distance);
                 transform.position = rayPoint;
             }
         }
     }

Comment
Add comment · Show 1
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 Basen · May 03, 2015 at 03:07 PM 0
Share

Bump?PleasE?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by misher · Mar 01, 2017 at 08:03 AM

 ///Here is a simple and tested component script that you can use on any 3d 
 ///(or even UI) object in order to detect Single or Double Tap gestures 
 ///(You will see in inspector 2 events). For 3d object there must be a collider, 
 ///also make sure there is Physics Raycaster component attached to the Camera, 
 ///if not - add one.
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 
 public class TapsEvents : MonoBehaviour, IPointerDownHandler, IBeginDragHandler {
 
     // You can add listeners in inspector
     public UnityEvent OnSingleTap;
     public UnityEvent OnDoubleTap;
 
 
     float firstTapTime = 0f;
     float timeBetweenTaps = 0.2f; // time between taps to be resolved in double tap
     bool doubleTapInitialized;
 
     public void OnPointerDown(PointerEventData eventData)
     {
         if (!doubleTapInitialized)
         {
             // invoke single tap after max time between taps
             Invoke("SingleTap", timeBetweenTaps);
             // init double tapping
             doubleTapInitialized = true;
             firstTapTime = Time.time;
         }
         else if (Time.time - firstTapTime < timeBetweenTaps)
         {
             // here we have tapped second time before "single tap" has been invoked
             CancelInvoke("SingleTap"); // cancel "single tap" invoking
             DoubleTap();
         }
     }
 
     void SingleTap()
     {
         doubleTapInitialized = false; // deinit double tap
 
         // fire OnSingleTap event for all eventual subscribers
         if(OnSingleTap != null)
         {
             OnSingleTap.Invoke();
         }
     }
 
     void DoubleTap()
     {
         doubleTapInitialized = false;
         if(OnDoubleTap != null)
         {
             OnDoubleTap.Invoke();
         }
     }
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         CancelInvoke("SingleTap");
         doubleTapInitialized = false;
     }
 }
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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Handling drag of an UI element in a scroll view over the whole canvas 1 Answer

Drag Object, same speed as mouse/touch 3 Answers

Touch - drag different objects 1 Answer

Multiple Cars not working 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