Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Gameobjects Moving on Swipe

Discussion in 'Scripting' started by gamecreator333, Jun 21, 2015.

  1. gamecreator333

    gamecreator333

    Joined:
    May 4, 2015
    Posts:
    21
    Hello Everyone!
    I need help on a script I am trying to make.

    here is my code so far

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class slider2 : MonoBehaviour {
    6.     public GameObject[] sprites;
    7.  
    8. private Vector2 fp; // first finger position
    9.     private Vector2 lp; // last finger position
    10.     private float angle;
    11.     private float swipeDistanceX;
    12.     private float swipeDistanceY;
    13.     List<GameObject> zones;
    14.  
    15.     void Start()
    16. {
    17.     GameObject Box1 = GameObject.Find("B1");
    18.     GameObject Box2 = GameObject.Find("B2");
    19.     GameObject Box3 = GameObject.Find("B3");
    20.     GameObject Box4 = GameObject.Find("B4");
    21.     zones = new List<GameObject>();
    22.     zones.Add(Box1);
    23.     zones.Add(Box2);
    24.     zones.Add(Box3);
    25.     zones.Add(Box4);
    26.     Debug.Log(zones.Count);
    27. }  
    28.  
    29.  
    30.     void Update () {
    31.         foreach(Touch touch in Input.touches)   //code for swipe
    32.         {
    33.             if (touch.phase == TouchPhase.Began)
    34.             {
    35.                 fp = touch.position;
    36.                 lp = touch.position;
    37.             }
    38.             if (touch.phase == TouchPhase.Moved )
    39.             {
    40.                 lp = touch.position;
    41.                 swipeDistanceX = Mathf.Abs((lp.x-fp.x));
    42.                 swipeDistanceY = Mathf.Abs((lp.y-fp.y));
    43.             }
    44.             if(touch.phase == TouchPhase.Ended)
    45.             {
    46.                 angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
    47.              
    48.                 if(angle > 60 && angle < 120 && swipeDistanceX > 40    )  // when there is a swipe right
    49.                 {
    50.                    Debug.Log("right");
    51.                    for (int i=0;i<zones.Count; i++){
    52.                        zones[i] = zones[(i-1)%zones.Count];
    53.                        zones[i].transform.position = new Vector2(i, 0);
    54.                  
    55.                 }
    56.  
    57.                 if(angle < -60 && angle > -120 && swipeDistanceX > 40)  //when there is a swipe left
    58.                 {
    59.                     Debug.Log("left");
    60.                     for (int i=0;i<zones.Count; i++){
    61.                         zones[i] = zones[(i+1)%zones.Count];
    62.                         zones[i].transform.position = new Vector2(i, 0);
    63.  
    64.                  
    65.            
    66.                          }
    67.         }
    68.     }
    69. }
    70.  
    71.     }
    72. }
    73. }

    Any help is appreciated.
     
    Last edited: Jun 30, 2015
  2. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    Hi , try this code (just replace your Update function with this one), its working but i don't know, is this what you wanted or not?

    Code (csharp):
    1.  
    2.  
    3. void Update () {
    4.  
    5. foreach(TouchtouchinInput.touches) //codeforswipe
    6. {
    7.   if (touch.phase == TouchPhase.Began)
    8. {
    9.   fp = touch.position;
    10.   lp = touch.position;
    11. }
    12. if (touch.phase == TouchPhase.Moved )
    13. {
    14.   lp = touch.position;
    15.   swipeDistanceX = Mathf.Abs((lp.x-fp.x));
    16.   swipeDistanceY = Mathf.Abs((lp.y-fp.y));
    17. }
    18. if(touch.phase == TouchPhase.Ended)
    19. {
    20.   angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
    21.  
    22.   if(angle > 60 && angle < 120 && swipeDistanceX > 40 ) //whenthereisaswiperight
    23.   {
    24.     Debug.Log("right");
    25.     Vector3tempposition = zones[0].transform.position;
    26.     for (inti=0;i<zones.Count; i++){
    27.     if(i == (zones.Count-1)){zones[i].transform.position = tempposition;}
    28.     else {zones[i].transform.position = zones[(i+1)%zones.Count].transform.position;}
    29.   }
    30. }
    31.  
    32. if(angle < -60 && angle > -120 && swipeDistanceX > 40) //whenthereisaswipeleft
    33. {
    34.   Debug.Log("left");
    35.   Vector3tempposition = zones[(zones.Count-1)].transform.position;
    36.   for (inti=(zones.Count-1);i>=0; i--){
    37.     print ((i+(zones.Count-1))%zones.Count);
    38.     if(i == (0)){zones[i].transform.position = tempposition;}
    39.     else {zones[i].transform.position = zones[(i+(zones.Count-1))%zones.Count].transform.position;}
    40.   }
    41. }
    42. }
    43.  
    44. }
    45. }
    46.  
    47.  
     
    Last edited: Jun 22, 2015
  3. gamecreator333

    gamecreator333

    Joined:
    May 4, 2015
    Posts:
    21
    Thanks, I will try tm that and get back to you asap.
     
  4. gamecreator333

    gamecreator333

    Joined:
    May 4, 2015
    Posts:
    21
    That works! Thanks so much!
    however there is one small issue. Whenever there is a swipe the objects then aren't correctly lined up horizontally. Some of them are a little too high or too much to the right or too much to the left or too low.
     
  5. muralisv

    muralisv

    Joined:
    Jan 19, 2015
    Posts:
    2
    Hi I am using unity with ouclus sdk,
    when using ovrplayercontroller in the scene, iam able to navigate the scene when using a game pad controller. But on the other hand, when i use the touchpad that is present in the Gear VR device, iam only able to rroate and view the scene.
    The horizontal and vertical axis using touchpad does not make the forward or backward movement or left and right movement,
    Could anyone please help me out on this.