- Home /
How to get intermediate swipe positions?
Hello guys, I'm working on a android swipe game where in I need to add a force based on the swipe direction. I have managed to get this working, but I'm stuck at a point where in I would like to get more touch positions other than the start position and the touch end position, say, I would like to get an intermediate position. how would I do that?
Below is the script which adds force to a sphere based on the swipe direction
 using UnityEngine;
 using System.Collections;
 
 public class SwipeControl : MonoBehaviour
 {
     //First establish some variables
     private Vector3 fp; //First finger position
     private Vector3 lp; //Last finger position
     private float dragDistance;  //Distance needed for a swipe to register
     public float power;
     private Vector3 footballPos;
     private bool canShoot = true;
     private float factor = 40f;
     void Start(){
 
         dragDistance = Screen.height*20/100;
         Physics.gravity = new Vector3(0, -20, 0);
         footballPos = transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         //Examine the touch inputs
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 fp = touch.position;
                 lp = touch.position;
 
             }
             if (touch.phase == TouchPhase.Moved)
             {
                 lp = touch.position;
                 Debug.Log(fp-lp);
             }
             if (touch.phase == TouchPhase.Ended)
             {
                 //First check if it's actually a drag
                 if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
                 {   //It's a drag
                     //Now check what direction the drag was
                     //First check which axis
                     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                     {   //If the horizontal movement is greater than the vertical movement...
                         if ((lp.x>fp.x) && canShoot)  //If the movement was to the right)
                         {   //Right move
                             float x = (lp.x - fp.x) / Screen.height * factor;
                             rigidbody.AddForce((new Vector3(x,10,16))*power);
                             Debug.Log("right "+(lp.x-fp.x));//MOVE RIGHT CODE HERE
                             canShoot = false;
                             //rigidbody.AddForce((new Vector3((lp.x-fp.x)/30,10,16))*power);
                             StartCoroutine(ReturnBall());
 
                         }
                         else
                         {   //Left move
                             float x = (lp.x - fp.x) / Screen.height * factor;
                             rigidbody.AddForce((new Vector3(x,10,16))*power);
                             Debug.Log("left "+(lp.x-fp.x));//MOVE LEFT CODE HERE
                             canShoot = false;
                             //rigidbody.AddForce(new Vector3((lp.x-fp.x)/30,10,16)*power);
                             StartCoroutine(ReturnBall());
                         }
                     }
                     else
                     {   //the vertical movement is greater than the horizontal movement
                         if (lp.y>fp.y)  //If the movement was up
                         {   //Up move
                             float y = (lp.y-fp.y)/Screen.height*factor;
                             float x = (lp.x - fp.x) / Screen.height * factor;
                             rigidbody.AddForce((new Vector3(x,y,16))*power);
                             Debug.Log("up "+(lp.x-fp.x));//MOVE UP CODE HERE
                             canShoot = false;
                             //rigidbody.AddForce(new Vector3((lp.x-fp.x)/30,10,16)*power);
                             StartCoroutine(ReturnBall());
                         }
                         else
                         {   //Down move
                             Debug.Log("down "+lp+" "+fp);//MOVE DOWN CODE HERE
                         }
                     }
                 }
                 else
                 {   //It's a tap
                     Debug.Log("none");//TAP CODE HERE
                 }    
             }
         }
     }
 
     IEnumerator ReturnBall() {
         
         yield return new WaitForSeconds(5.0f);
         rigidbody.velocity = Vector3.zero;
         rigidbody.angularVelocity = Vector3.zero;
         transform.position = footballPos;
         canShoot =true;
     }
 
 }
Answer by VesuvianPrime · Jul 21, 2014 at 11:36 AM
You can make a list property and add each touch position in TouchPhase.Moved to that list:
 using System.Collections.Generic;
 
 private List<Vector3> m_TouchPositions = new List<Vector3>();
 
 // During TouchPhase.Moved
 m_TouchPositions.Add(touch.position);
You can also simplify your code by using this list to get your first and last touch positions.
@VesuvianPrime does it mean that I need not use the TouchPhase.Began condition?
Your answer
 
 
             Follow this Question
Related Questions
TouchPhase doesn't end when player swipes off screen 1 Answer
Reset touch Vector after movement 2 Answers
How to move player once per swipe 2 Answers
Unity Touch Help! 0 Answers
How to detect whether a particular touch has ended? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                