Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Stylus drag and drop

Discussion in 'Scripting' started by HeatWave_Games, Oct 4, 2016.

  1. HeatWave_Games

    HeatWave_Games

    Joined:
    Jun 3, 2016
    Posts:
    52
    Hi,

    I have a script that drags and drops certain objects in my game.

    I use OnMouseDown, OnMouseDrag and OnMouseUp for mouse and the script below for touchscreen:

    Code (CSharp):
    1.  
    2. #elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8                            
    3.  
    4.     GameObject gameObjectToDrag;      
    5.     Vector3 GOcenter;
    6.     Vector3 touchPosition;
    7.     Vector3 newGOCenter;
    8.  
    9.     bool draggingMode = false;
    10.  
    11.     Ray ray;
    12.     RaycastHit hit;
    13.  
    14.     private void Update() {
    15.      
    16.         foreach (Touch touch in Input.touches) {
    17.             switch (touch.phase) {
    18.                 case TouchPhase.Began:
    19.                     Ray ray = Camera.main.ScreenPointToRay(touch.position);
    20.                     if (Physics.SphereCast(ray, 0.3f, out hit)) {
    21.                         gameObjectToDrag = hit.collider.gameObject;
    22.                         GOcenter = gameObjectToDrag.transform.position;
    23.                         touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    24.                         offset = touchPosition - GOcenter;
    25.                         draggingMode = true;
    26.                     }
    27.                     break;
    28.  
    29.                 case TouchPhase.Moved:
    30.                     if (draggingMode) {
    31.                         touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    32.                         newGOCenter = touchPosition - offset;
    33.                         gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, GOcenter.z);
    34.                     }
    35.                     break;
    36.  
    37.                 case TouchPhase.Ended:
    38.                     GroupReleased();
    39.                     draggingMode = false;
    40.                     break;
    41.             }
    42.         }
    43.     }
    44.  
    45. #endif
    How could I modify my code so that it could handle stylus input?

    I don't want to use the stylus buttons or anything, just a simple drag-and-drop with the stylus, because currently this code does not recognize stylus input.

    Thanks in advance.
     
  2. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    Have you tried disabling Touch Input Module, and enable on EventSystems and activate the Allow Activation on Mobile Device in Standalone Input Module ?
     
  3. HeatWave_Games

    HeatWave_Games

    Joined:
    Jun 3, 2016
    Posts:
    52
    Hi yepfuk,

    No, I haven't. Where can I set all these?

    Thanks.
     
  4. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    Look to the hierarchy window, find EventSystems, click this and then look to the Inspector window.