- Home /
 
               Question by 
               laurienash · Jul 06, 2016 at 12:56 AM · 
                androidtouchtouchphasedrag objects  
              
 
              Disable touch while object moving back to start position
Hi,
When the player swipes a finger up the screen, I want an object to be dragged up the screen on the y axis. When the touch input stops moving, or is a small value, I want the object to move back to the start location - and the player to be unable to drag the object back up until the object has reached the start location.
This is the script I have written - but the problem I'm having is that when movetocentre is true, the player is still able to move the object up, causing a really jerky movement.
Does anyone have any pointers on how I am able to fix this?
 using UnityEngine;
 using System.Collections;
 
 public class TouchMovement : MonoBehaviour {
     
     float speed = 0.5f;
     public float radius = 1.5f;
     public PerspectiveSwitchFinalSepCo persp;
     Vector3 centrePt = new Vector3(0,0,0);
     public bool moveToCentre = false;
     public GameObject bag;
     
     
     public AccelerometerMagLowPass accel;
     
     public Vector3 bagCurrentPos;
     public float bagCurrentPosFloat;
 
     
     
     void Update() 
     {
         if (persp.orthoOn == true)
         {
             
             centrePt.x = transform.position.x;
             centrePt.y = transform.position.y;
             
             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
             {
                 if (Input.GetTouch(0).deltaPosition.y >= 0.5)
                 {
                     speed = 0.1f;
                     float dragValue = ((Input.GetTouch(0).position.magnitude / Input.GetTouch(0).deltaTime)/1000);
                     
                     Vector3 movement = new Vector3(0,0, dragValue); 
                     Vector3 newPos = transform.position  + movement * Time.deltaTime * speed; 
                     Vector3 offset = newPos - centrePt; 
 
                     transform.position = new Vector3 (0, 0, Mathf.Clamp(offset.z, 0f, 4f));
                     
         
                 }
                 
                 else if (Input.GetTouch(0).deltaPosition.y <= -0.5) 
                 {
                     speed = 0f;
                     moveToCentre = true;
                     
                 }
                 
                 else if ((Input.GetTouch(0).deltaPosition.y > -0.5) &&  (Input.GetTouch(0).deltaPosition.y < 0.5))
                 {
                     speed = 0f;
                     moveToCentre = true;
                     
                 }
             }
             
             
             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
             {
                 
                 bagCurrentPosFloat = this.transform.position.z;
                 
                 moveToCentre = true;
                 
             }
             
             if (moveToCentre)
             {
                 transform.position = Vector3.MoveTowards(transform.position, centrePt, Time.deltaTime * 10f);
 
                 
                 
                 
                 
                 if (Vector3.Distance(transform.position, centrePt) <= 0.1)
                 {
                     moveToCentre = false;
                     transform.position = centrePt;
                 }
             }    
         }    
     }
 }
Thanks in advance!
Best, Laurien
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                