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 falconer · Jul 21, 2014 at 10:51 AM · touchtouch controlsswipetouchphase

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;
     }
 
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · 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
avatar image falconer · Jul 21, 2014 at 12:10 PM 0
Share

@VesuvianPrime does it mean that I need not use the TouchPhase.Began condition?

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

23 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 avatar image

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


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