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 Omer.Hussain · Jan 02, 2014 at 02:34 PM · movementtouchtouchscreenswipe

Help with swiping for movement

Hi! i am new to Unity, can any please tell me what is wrong with this script, its for swiping for right,left,up,down. what i want when i swipe up it print("up") in log, same i want for left right.doesn't show up in log, actually i want my character to move rite left jump and down and i have script for that. kindly help .. :'(

 #pragma strict
 
 
 
 //I use the following script, that is attached to my character (something like a chess pawn) and will move it on a board horizontally or vertically, depending on the swipes. The swipes can be made anywhere on the screen and you don't have to make them on the character.
 
 
 var comfortZoneVerticalSwipe: float = 50; // the vertical swipe will have to be inside a 50 pixels horizontal boundary
 var comfortZoneHorizontalSwipe: float = 50; // the horizontal swipe will have to be inside a 50 pixels vertical boundary
 var minSwipeDistance: float = 14; // the swipe distance will have to be longer than this for it to be considered a swipe
 //the following 4 variables are used in some cases that I don’t want my character to be allowed to move on the board (it’s a board game)
 var allowGoUp: boolean = true;
 var moving: boolean = true;
 var allowGoRight: boolean = true;
 var allowGoLeft: boolean = true;
 var allowGoDown: boolean = true;
 var maxSwipeTime: float= 0.5;
 
 
 function Update () {
 if (Input.touchCount >0) {
 var touch = Input.touches[0];
 
 switch (touch.phase) { //following are 2 cases
 case TouchPhase.Began: //here begins the 1st case
 var startPos = touch.position;
 var startTime = Time.time;
 
 break; //here ends the 1st case
 
 
 
 case TouchPhase.Ended: //here begins the 2nd case
 var swipeTime = Time.time - startTime;
 var swipeDist = (touch.position - startPos).magnitude;
 var endPos = touch.position;
 
 if ((Mathf.Abs(touch.position.x - startPos.x))<comfortZoneVerticalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.y - startPos.y)>0 && !moving && transform.position.z<3 && allowGoUp)
 {
 //... then go up
 moving=true;
 print("up");
 //[code here, to make character move the way you want (upwards)]
 }
 
 
 if ((Mathf.Abs(touch.position.x - startPos.x))<comfortZoneVerticalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.y - startPos.y)<0 && !moving && transform.position.z>-3 && allowGoDown)
 {
 //... then go down
 moving=true;
 //[code here, to make character move the way you want (downwards)]
 }
 
 
 if ((Mathf.Abs(touch.position.y - startPos.y))<comfortZoneHorizontalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.x - startPos.x)<0 && !moving && transform.position.x>-2 && allowGoLeft)
 {
 //... then go left
 moving=true;
 //[code here, to make character move the way you want (to the left)]
 }
 
 if ((Mathf.Abs(touch.position.y - startPos.y))<comfortZoneHorizontalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.x - startPos.x)>0 && !moving && transform.position.x<2 && allowGoRight)
 {
 //...then go right
 moving=true;
 //[code here, to make character move the way you want (to the right)]
 }
 break; //here ends the 2nd case
 
 }
 }
 }
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

Answer by sillanstudios · Jan 02, 2014 at 05:14 PM

Hi Omer, What I did when I came across a situation like this, was I did the following: I created a GUITexture to define the area I wanted the swipes in. I then used this bit of code in a script placed on the GUITexture.

var startPos : int; var endPos : int; var swipeThreshold : int;

function Update(){ for (var i = 0; i < Input.touchCount; i++){ var touch = Input.GetTouch(i); var hold = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) { startPos = touch.position; } if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(touch.position) || touch.phase == TouchPhase.Canceled) { endPos = touch.position; if(endPos.x > startPos.x){ if((endPos.x - startPos.x)> swipeThreshold){ //we have a right swipe } } if(endPos.x < startPos.x){ if((endPos.x - startPos.x)* -1 > swipeThreshold){ //we have a left swipe } } } } }

This is what I did for left and right swipes and you can do the same for up and down by changing the the x to a y. The threshold is the minimum distance that can be counted as a swipe. I hope this helped. Please mark it as correct if it works :D Johnny

Comment
Add comment · Show 2 · 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 Omer.Hussain · Jan 03, 2014 at 06:33 AM 0
Share

@Johnny thanks i applied the code of my own which is quite related with your code :p . it shows right and left in log but my character doesn't move to the right position neither left. i have applied position change through lerp , i want to move it in exact right left positions and for middle as well. position change work in seperate script code but when i apply in this script it doesn't work. i don't know what's the problem. #pragma strict

 var position$$anonymous$$ : Vector3 = new Vector3(39.90576,1.292813,1.095224);
 var positionR : Vector3 = new Vector3(39.90576,1.292813,7.095224);
 var positionL : Vector3 = new Vector3(39.90576,1.292813,-3.095224);
 var startTouch : Vector2;
 public var smooth : float;
 private var newPosition : Vector3;
 
 function Awake ()
 {
     newPosition = transform.position;  
 }
 
 function Update ()
 {
     //PositionChanging();
     
    if (Input.touchCount > 0)
    { 
             //print("start");
                     var touch : Touch = Input.GetTouch(0);
                 if(touch.phase == TouchPhase.Began)
                  {
              //print("began"+newPosition);
                      startTouch = touch.position;
                  //    print("start position is: " +touch.position);
                 }
                             
             if(touch.phase == TouchPhase.$$anonymous$$oved) 
             {
         //    print("moved");            
             }    
             
             if(touch.phase == TouchPhase.Ended) 
             {
             /////////////////////for right move////////////////
             if(startTouch.x-100 > touch.position.x)
             {
             
                         //Right();
                          //newPosition = transform.position; 
                          newPosition = positionL;
                          
                         print("left agya");
             }
             
             /////////////////////for left move////////////////
             if(startTouch.x+100 < touch.position.x)
             {
                         print("right gya");
                         newPosition = positionR;
                         
             }
         }                
     }
      transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
 }
avatar image Omer.Hussain · Jan 03, 2014 at 06:39 AM 0
Share

and by mistake i have written right for left move and left for right move :p ... but code works for right and left correctly

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

19 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

Related Questions

swipe distance detection dont work 2 Answers

Throw an object via touch/mouse flick 0 Answers

How do I move an object with my finger?[C#] 1 Answer

Swipe movement not working correctly. 2 Answers

Different resolutions make the swipe of my player go at different speeds on different devices 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