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 Pritchy · Jul 26, 2012 at 04:21 PM · movementtouchscreenswipe

Swipe movement not working correctly.

Hi, I have an object, controlled by the player via a custom script, and when swipe is detected, control is taken away from the player and it moves along in a straight line until a collision occurs, where the object stops and control is returned to the player. The script, a modified version of one found on the internet, records the first and last position of a swipe and then compares them in various ways to find if it was an up, down, left or right swipe. the problem I am having is that the swipe detection is very weak: sometimes it ends the swipe early, and it often does not recognise the swipe whatsoever. I assumed this was something to do with the swipeThresh variable, however changing this does not perfect the movement. I'm looking for something that works consistently. Is there anything that can be done with this script to make it reliable, or is there another way of doing it? Thanks in advance, Tom.

     // Declaration of Variables
 
         //These are the movement booleans. When one of these are true, the Player Object moves in that direction.
         var Right: boolean;
         var Left: boolean;
         var Up: boolean;
         var Down: boolean;
         //This is the speed of the Player Object.
         var MoveSpeed: float = 7;
         //This is so we can move the position of the Collider Mesh of the Player Object around.
         var myCollider: BoxCollider;
         //Stop Screen switching off.
         Screen.sleepTimeout = 0;
         //Swipe Movement Vars
  var  swipeThresh      :    float  =   1.2;
  var  swipeStart       :  Vector2  =   Vector2.zero;
  var  swipeEnd         :  Vector2  =   Vector2.zero;
  var  swipeWasActive   :  boolean  =   false;
  var  Subtraction      :  Vector2;
  
  
 //Movement 
 
  function Update ()
  {
  
      if ( Input.touchCount == 1 ) {
          processSwipe();
          }
  
  
 //Activating the Booleans
 
  //Swipe
  if (Subtraction.x > 0 && Mathf.Abs(Subtraction.x) > Mathf.Abs(Subtraction.y)&& !Left && !Up && !Down && myCollider.center!=Vector3(0,0,0.15)){
  Right = true;
  }
     if (Subtraction.x < 0 && Mathf.Abs(Subtraction.x) > Mathf.Abs(Subtraction.y) && !Right && !Up && !Down && myCollider.center!=Vector3(0,0,-0.15)){
  Left = true;
  }
      if (Subtraction.y > 0 && Mathf.Abs(Subtraction.y) > Mathf.Abs(Subtraction.x) && !Left && !Right && !Down && myCollider.center!=Vector3(-0.15,0,0)){
  Up = true;
  }
      if (Subtraction.y < 0 && Mathf.Abs(Subtraction.y) > Mathf.Abs(Subtraction.x) && !Left && !Up && !Right && myCollider.center!=Vector3(0.15,0,0)){
  Down = true;
  }
  
  //Arrow Keys
     //If the User presses the key, no other Boolean is true and the Collision Mesh of the Player Object
     //is not already at the specified direction, activate the boolean.
     
      if(Input.GetKey("right") && !Left && !Up && !Down && myCollider.center!=Vector3(0,0,0.15)){
      Right = true;
      }
  
      if(Input.GetKey("left") && !Right && !Up && !Down && myCollider.center!=Vector3(0,0,-0.15)){
      Left = true;
      }
  
      if(Input.GetKey("up") && !Left && !Right && !Down && myCollider.center!=Vector3(-0.15,0,0)){
      Up = true;
      }
      
      if(Input.GetKey("down") && !Left && !Up && !Right && myCollider.center!=Vector3(0.15,0,0)){
      Down = true;
      }
     
     
 //Actual Movement
 //If the Boolean is true, move the Player Object and the Collision Mesh.
  
  //Right
         if (Right) {
         transform.Translate(Vector3(0,0,MoveSpeed) * Time.deltaTime);
         myCollider.center = Vector3(0,0,0.15);
         }
 
  //Left
 
         if (Left) {
         transform.Translate(Vector3(0,0,-MoveSpeed) * Time.deltaTime);
         myCollider.center = Vector3(0,0,-0.15);
         }
 
  //Up
 
         if (Up) {
         transform.Translate(Vector3(-MoveSpeed,0,0) * Time.deltaTime);
         myCollider.center = Vector3(-0.15,0,0);
         }
 
  //Down
 
         if (Down) {
         transform.Translate(Vector3(MoveSpeed,0,0) * Time.deltaTime);
         myCollider.center = Vector3(0.15,0,0);
         }
  }
  
  function OnGUI ()
  {
      processSwipe();
      drawStartBox();
      drawEndBox();
  }
  
  function processSwipe ()
  {
      if ( Input.touchCount != 1 ) {
      Subtraction = swipeEnd - swipeStart;
          return;
      }
  
      var theTouch : Touch = Input.touches[0];
  
      /* skip the frame if deltaPosition is zero */
  
      if ( theTouch.deltaPosition == Vector2.zero ) {
          return;
      }
  
      var speedVec : Vector2 = theTouch.deltaPosition * theTouch.deltaTime;
      var theSpeed :   float = speedVec.magnitude;
  
      var swipeIsActive : boolean = ( theSpeed > swipeThresh );
  
      if ( swipeIsActive ) {
  
          if ( ! swipeWasActive ) {
              swipeStart = theTouch.position;
          }
      }
  
      else {
  
          if ( swipeWasActive ) {
              swipeEnd = theTouch.position;
              Debug.Log("Swipe Complete");
          }
      }
  
      swipeWasActive = swipeIsActive;
  }
  
  function drawStartBox ()
  {
      if ( swipeStart == Vector2.zero ) {
          return;
      }
  
      /* don't forget to invert the y-coordinate */
  
      var theY = Screen.height - swipeStart.y;
      var theX = swipeStart.x;
  
      var theW = 140;
      var theH =  40;
  
      var theRect : Rect = Rect(theX, theY, theW, theH);
  
      GUI.Label(theRect, "Start");
  }
  
  function drawEndBox ()
  {
      if ( swipeEnd == Vector2.zero ) {
          return;
      }
  
      /* don't forget to invert the y-coordinate */
  
      var theY = Screen.height - swipeEnd.y;
      var theX = swipeEnd.x;
  
      var theW = 140;
      var theH =  40;
  
      var theRect : Rect = Rect(theX, theY, theW, theH);
  
      GUI.Label(theRect, "End");
  }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Platinium · Jul 26, 2012 at 06:28 PM

Well i had a similar problem with my Swipe function, and a made a very simple script that worked pretty well for me(up,down,left,right), i record the first touch point and during the movement of the finger i am checking the directions of the vector(first point and last) relatively to the first touch point and depending on the angle, i get the actual direction:

         Vector2 deltaPosition;
         Vector2 afterDeltaPosition;
         float angle;
         
         void FixedUpdate()
         {
             if(Input.touchCount == 1)
             { 
                 //its always x=-1 and y=-1 if touchCount == 0 Resets
                 if(deltaPosition.x == -1 && deltaPosition.y == -1)
                     deltaPosition = Input.GetTouch(0).position; //First touch  point stored
                 afterDeltaPosition = Input.GetTouch(0).position;//the swiping points
                 angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ; // angle in degrees from 0,-180 and 0,180)
                 Debug.Log("Angle "+ angle);
                 if(angle < 45f && angle > -45f)
                 {
                     if(anim.isPlaying() && anim.CurrentClip.name != "right")
                 {
                 anim.Play("right"); 
                 rigidbody.velocity = (Vector3.right * speed);
             }
         }
         else if(angle < 135f && angle > 45f)
         {
             if(anim.isPlaying() && anim.CurrentClip.name != "front")
             {
                 anim.Play("back"); 
                 rigidbody.velocity = (Vector3.up * speed);
             }
         }
         else if(angle > 135f || angle < -135f)
         {
             if(anim.isPlaying() && anim.CurrentClip.name != "left")
             {
                 anim.Play("left"); 
                 rigidbody.velocity = (Vector3.left * speed); 
             }
         }
         else if(angle > -135f && angle < -45f)
         {
             if(anim.isPlaying() && anim.CurrentClip.name != "front")
             {
                 anim.Play("front"); 
                 rigidbody.velocity = (Vector3.down * speed);
             }
         }
     }
     else if( Input.touchCount == 0)
     {
         if(deltaPosition.x != -1 && deltaPosition.y != -1)
         {
             deltaPosition.x=-1;
             deltaPosition.y=-1;
         }
     }
 }
Comment
Add comment · Show 4 · 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 Pritchy · Jul 29, 2012 at 07:38 PM 0
Share

Thank you Platinium, testing out now!!

avatar image Pritchy · Aug 06, 2012 at 10:11 PM 0
Share

Just a quick update, haven't forgotten to thank you, been really busy as of late and that coupled with a few kinks in the code has delayed this response. Thanks for the code, works much better than my original code, has one major bug left buy I'm sure I can squash it! thanks, $$anonymous$$

avatar image Bunny83 · Aug 12, 2012 at 12:54 AM 0
Share

I've fixed the formatting which was horrible, but it seems you actually totally messed up your brackets at least they doesn't make much sense that way.

avatar image Pritchy · Aug 12, 2012 at 03:45 AM 0
Share

Thank you for your help Bunny83, I can only apologise for naff formatting or bad code, my only excuse being I'm rather new to the scripting stuff! No excuse though, I'll try to format my code in that manner in future, thanks again, $$anonymous$$

avatar image
0

Answer by Pritchy · Aug 12, 2012 at 12:41 AM

The code here works very, very well and I thank you for giving me such a solid piece of code, however there is one problem; the player always wants to move to the right. I think this is due to setting the deltas to minus one, however I am unsure. Could you perhaps look and see if you can tell where I'm going wrong? Thanks again for all of your help, Tom.

 //Movement
 //These are the movement booleans. When one of these are true, the Player Object moves in that direction.
 var Right: boolean;
 var Left: boolean;
 var Up: boolean;
 var Down: boolean;
 //This is the speed of the Player Object.
 var MoveSpeed: float = 14; 
 //This is so we can move the position of the Collider Mesh of the Player Object around.
 var myCollider: BoxCollider;
 
 //Swipe
 var deltaPosition: Vector2;
 var afterDeltaPosition: Vector2;
 var angle: float;
 
 function FixedUpdate()
 {    
     if(Input.touchCount == 1)
     {
         //its always x=-1 and y=-1 if touchCount == 0 Resets
         if(deltaPosition.x==-1&& deltaPosition.y==-1)
             deltaPosition = Input.GetTouch(0).position; //First touch point stored.
         afterDeltaPosition = Input.GetTouch(0).position;//The swiping points.
         angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ; // angle in degrees from 0,-180 and 0,180)
         Debug.Log("Angle"+ angle);
         
         if(angle < 45f && angle > -45f)
         {
             if (!Left && !Up && !Down && myCollider.center != Vector3(0,0,0.15))
             { 
                 Right = true;
                 deltaPosition.x = -1; 
                 deltaPosition.y = -1;
                 afterDeltaPosition.x = 0;
                 afterDeltaPosition.y = 0;
                 Moving.Play();
             }
         }
         else if(angle < 135f && angle > 45f)
         {
             if (!Left && !Up && !Right && myCollider.center != Vector3(0.15,0,0))
             { 
                 Up = true;
                 deltaPosition.x = -1; 
                 deltaPosition.y = -1;
                 afterDeltaPosition.x = 0;
                 afterDeltaPosition.y = 0;
                 Moving.Play();
             }
         }
         else if(angle > 135f || angle < -135f)
         {
             if (!Right && !Up && !Down && myCollider.center != Vector3(0,0,-0.15))
             { 
                 Left = true;
                 deltaPosition.x = -1; 
                 deltaPosition.y = -1;
                 afterDeltaPosition.x = 0;
                 afterDeltaPosition.y = 0;
                 Moving.Play();
             }
         }
         else if(angle > -135f && angle < -45f)
         {
             if (!Left && !Right && !Down && myCollider.center != Vector3(-0.15,0,0))
             { 
                 Down = true;
                 deltaPosition.x = -1; 
                 deltaPosition.y = -1;
                 afterDeltaPosition.x = 0;
                 afterDeltaPosition.y = 0;
                 Moving.Play();
             }
         }
     }
     else if( Input.touchCount == 0)
     {
         if(deltaPosition.x != -1 && deltaPosition.y != -1)
         {
             deltaPosition.x = -1;
             deltaPosition.y = -1;
         }
     }
 }
 
 function Update ()
 {
     //Arrow Keys
     //If the User presses the key, no other Boolean is true and the Collision Mesh of the Player Object
     //is not already at the specified direction, activate the boolean.
     if(Input.GetKey("right") && !Left && !Up && !Down && myCollider.center != Vector3(0,0,0.15))
     {
         Right = true;
         Moving.Play();
     }
     
     if(Input.GetKey("left") && !Right && !Up && !Down && myCollider.center != Vector3(0,0,-0.15))
     {
         Left = true;
         Moving.Play();
     }
     
     if(Input.GetKey("up") && !Left && !Right && !Down && myCollider.center != Vector3(-0.15,0,0))
     {
         Up = true;
         Moving.Play();
     }
     
     if(Input.GetKey("down") && !Left && !Up && !Right && myCollider.center != Vector3(0.15,0,0))
     {
         Down = true;
         Moving.Play();
     }
  
     //Actual Movement
     //If the Boolean is true, move the Player Object and the Collision Mesh.
     if (Right)
     {
         transform.Translate(Vector3(0,0,MoveSpeed) * Time.deltaTime);
         myCollider.center = Vector3(0,0,0.15);
     }
     
     if (Left)
     {
         transform.Translate(Vector3(0,0,-MoveSpeed) * Time.deltaTime);
         myCollider.center = Vector3(0,0,-0.15);
     }
     
     if (Up)
     {
         transform.Translate(Vector3(-MoveSpeed,0,0) * Time.deltaTime);
         myCollider.center = Vector3(-0.15,0,0);
     }
     
     if (Down)
     {
         transform.Translate(Vector3(MoveSpeed,0,0) * Time.deltaTime);
         myCollider.center = Vector3(0.15,0,0);
     }
 }
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with swiping for movement 1 Answer

Object transported to point instead of moving towards it? 1 Answer

HELP! How to make an object move until colliding with another object. 2 Answers

swipe movement script like in subway surfer 0 Answers

How to make a Calligraphy learning application 0 Answers


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