- Home /
 
Disable Swipe Controls For Limited Time?
Hello, l have a game and the player is a cube, pretty basic and moves with swipe controls on Android and IOS and on computer it uses the mouse swipes as that's the closest thing to a swipe. The controls work fine, but what l want to do is, for example, disable the up swipe when my player is against the wall so he doesn't hit into the wall. So, my player already moves one step in the direction of the swipe, however when he is one step away from the wall then disable the swipe controls so the player can't glitch out and go into the wall or float in the air.
I have two scripts for my Cube:
One - CubeController:
 using UnityEngine;
 using System.Collections;
 
 public class CubeController : MonoBehaviour
 {
     //Speed of the player
     public float m_playerSpeed = 0.5f;  
 
     //Size of the player
     public float m_playerSize = 1f;
 
     //Move the cube only when last movement is finished/stopped
     private bool m_isMoving = false;  
 
     //Start Y axis positon
     private float m_startY = 0f; 
 
     //Child object name from cube
     private const string TARGET_POINT = "TargetPoint";
 
     void Update ()
     {
         //Control methods
         if (SwipeController.isUp() || SwipeController.isTap())  
         {  
             moveUp();
         }   
         if (SwipeController.isDown())  
         {  
             moveDown();
         }  
         if (SwipeController.isLeft())  
         {  
             moveLeft();
         }  
         if (SwipeController.isRight())  
         {  
             moveRight();
         }  
     } 
 
     // Special movement method for rolling over the edges of the cube
     private IEnumerator DoRoll (Vector3 point, Vector3 axis, float angle)
     {    
         float steps = Mathf.Ceil(m_playerSpeed * 30.0f);  
         float angleStep = angle / steps;  
 
         // Rotate the cube by the point, axis and angle  
         for (int i= 1; i <= steps; i++)   
         {   
             transform.RotateAround (point, axis, angleStep);  
             yield return new WaitForSeconds(0.0033333f);  
         }   
         
         // move the TARGET_POINT to the center of the cube   
         transform.Find(TARGET_POINT).position = transform.position;  
         
         // Make sure the y position is correct 
         Vector3 pos = transform.position;  
         pos.y = m_startY;  
         transform.position = pos;  
         
         // Make sure the angles are snaping to 90 degrees.       
         Vector3 vec= transform.eulerAngles;  
         vec.x = Mathf.Round(vec.x / 90) * 90;  
         vec.y = Mathf.Round(vec.y / 90) * 90;  
         vec.z = Mathf.Round(vec.z / 90) * 90;  
         transform.eulerAngles = vec;  
         
         // The cube is stoped  
         m_isMoving = false;       
     }  
 
     //### Player movement methods ###
     private void moveUp()
     {
         if (m_isMoving)
         {
             return;
         }
         m_isMoving = true;  
         transform.Find(TARGET_POINT).Translate(0, -m_playerSize/2 , m_playerSize/2, Space.Self);  
         StartCoroutine(DoRoll(transform.Find(TARGET_POINT).position, Vector3.right, 90.0f));
     }
 
     private void moveDown()
     {
         if (m_isMoving)
         {
             return;
         }
         m_isMoving = true;  
         transform.Find(TARGET_POINT).Translate(0, -m_playerSize/2, -m_playerSize/2, Space.Self);  
         StartCoroutine(DoRoll(transform.Find(TARGET_POINT).position, -Vector3.right, 90.0f));
     }
 
     private void moveLeft()
     {
         if (m_isMoving)
         {
             return;
         }
         m_isMoving = true;  
         transform.Find(TARGET_POINT).Translate(-m_playerSize/2, -m_playerSize/2, 0, Space.Self);  
         StartCoroutine(DoRoll(transform.Find(TARGET_POINT).position, Vector3.forward, 90.0f));
     }
 
     private void moveRight()
     {
         if (m_isMoving)
         {
             return;
         }
         m_isMoving = true;  
         transform.Find(TARGET_POINT).Translate(m_playerSize/2, -m_playerSize/2, 0, Space.Self);  
         StartCoroutine(DoRoll(transform.Find(TARGET_POINT).position, -Vector3.forward, 90.0f));    
     }
 }
 
               And my other script is SwipeController:
 using UnityEngine;
 using System.Collections;
 
 public class SwipeController : MonoBehaviour
 {
     // Min length to detect the Swipe
     public float m_minSwipeLength = 35f;
 
     // Should debug messages be printed to Debug.log
     public bool m_debugEnabled = false;
     
     private Vector2 m_firstPressPos;
     private Vector2 m_secondPressPos;
     private Vector2 m_currentSwipe;    
     private Vector2 m_firstClickPos;
     private Vector2 m_secondClickPos;
     
     private static ESwipeDirection s_swipeDirection = ESwipeDirection.None;
 
     private void Update()
     {
         DetectSwipe();
     }
     
     private void DetectSwipe()
     {
         if ( Input.touchCount > 0 )
         {
             Touch t = Input.GetTouch( 0 );
             
             if ( t.phase == TouchPhase.Began )
             {
                 m_firstPressPos = new Vector2( t.position.x, t.position.y );
             }
             
             if ( t.phase == TouchPhase.Ended )
             {
                 m_secondPressPos = new Vector2( t.position.x, t.position.y );
                 m_currentSwipe = new Vector3( m_secondPressPos.x - m_firstPressPos.x, m_secondPressPos.y - m_firstPressPos.y );
 
                 writeDebug("Swipe length: " + m_currentSwipe.magnitude);
                 // Make sure it was a real swipe and not a single tap
                 if ( m_currentSwipe.magnitude < m_minSwipeLength )
                 {
                     s_swipeDirection = ESwipeDirection.Tap;
                     writeDebug( "Tap" );
                     return;
                 }
                 
                 m_currentSwipe.Normalize();
 
                 //Swipe direction checks
                 
                 // Swipe up
                 if ( m_currentSwipe.y > 0 && m_currentSwipe.x > -0.5f && m_currentSwipe.x < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Up;
                     writeDebug("Up");
                 }
                 // Swipe down
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x > -0.5f && m_currentSwipe.x < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Down;
                     writeDebug("Down");
                 }
                 // Swipe left
                 else if ( m_currentSwipe.x < 0 && m_currentSwipe.y > -0.5f && m_currentSwipe.y < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Left;
                     writeDebug("Left");
                 }
                 // Swipe right
                 else if ( m_currentSwipe.x > 0 && m_currentSwipe.y > -0.5f && m_currentSwipe.y < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Right;
                     writeDebug("Right");
                 }
                 // Swipe up left
                 else if ( m_currentSwipe.y > 0 && m_currentSwipe.x < 0 )
                 {
                     s_swipeDirection = ESwipeDirection.UpLeft;
                     writeDebug("UpLeft");
                 }
                 // Swipe up right
                 else if ( m_currentSwipe.y > 0 && m_currentSwipe.x > 0 )
                 {
                     s_swipeDirection = ESwipeDirection.UpRight;
                     writeDebug("UpRight");
                 }
                 // Swipe down left
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x < 0 )
                 {
                     s_swipeDirection = ESwipeDirection.DownLeft;
                     writeDebug("DownLeft");
                 }    
                 // Swipe down right
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x > 0 )
                 {
                     s_swipeDirection = ESwipeDirection.DownRight;
                     writeDebug("DownRight");
                 }
             }
         }
         else
         {
             //Mouse detection part for simulating swipe control
             if (Input.GetMouseButtonDown(0))
             {
                 m_firstClickPos = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
             }
             else
             {
                 s_swipeDirection = ESwipeDirection.None;
             }
             if (Input.GetMouseButtonUp(0))
             {
                 //mouse button released
                 m_secondClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                 m_currentSwipe = new Vector3(m_secondClickPos.x - m_firstClickPos.x, m_secondClickPos.y - m_firstClickPos.y);
                 
                 // Make sure it was a real swipe and not a single tap
                 if ( m_currentSwipe.magnitude < m_minSwipeLength)
                 {
                     s_swipeDirection = ESwipeDirection.Tap;
                     writeDebug("Tap");
                     return;
                 }
                 
                 m_currentSwipe.Normalize();
                 
                 //Swipe direction checks
 
                 // Swipe up
                 if ( m_currentSwipe.y > 0 && m_currentSwipe.x > -0.5f && m_currentSwipe.x < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Up;
                     writeDebug("Up");
                 }
                 // Swipe down
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x > -0.5f && m_currentSwipe.x < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Down;
                     writeDebug("Down");
                 }
                 // Swipe left
                 else if ( m_currentSwipe.x < 0 && m_currentSwipe.y > -0.5f && m_currentSwipe.y < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Left;
                     writeDebug("Left");
                 }
                 // Swipe right
                 else if ( m_currentSwipe.x > 0 && m_currentSwipe.y > -0.5f && m_currentSwipe.y < 0.5f )
                 {
                     s_swipeDirection = ESwipeDirection.Right;
                     writeDebug("Right");
                 }     // Swipe up left
                 else if ( m_currentSwipe.y > 0 && m_currentSwipe.x < 0 )
                 {
                     s_swipeDirection = ESwipeDirection.UpLeft;
                     writeDebug("UpLeft");                    
                 }
                 // Swipe up right
                 else if ( m_currentSwipe.y > 0 && m_currentSwipe.x > 0 )
                 {
                     s_swipeDirection = ESwipeDirection.UpRight;
                     writeDebug("UpRight");                    
                 }
                 // Swipe down left
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x < 0 )
                 {
                     s_swipeDirection = ESwipeDirection.DownLeft;
                     writeDebug("DownLeft");
                 }
                 // Swipe down right
                 else if ( m_currentSwipe.y < 0 && m_currentSwipe.x > 0 )
                 {
                     s_swipeDirection = ESwipeDirection.DownRight;
                     writeDebug("DownRight");
                 }
             }
         }
     }
 
     // write debug output if enabled
     private void writeDebug(string message)
     {
         if (!m_debugEnabled)
         {
             return;
         }
         Debug.Log(message);
     }
 
     // ### Public control methods to check swipe direction ###
     public static bool isTap()
     {
         return s_swipeDirection.Equals(ESwipeDirection.Tap);
     }
     
     public static bool isUp()
     {
         return s_swipeDirection.Equals(ESwipeDirection.Up);
     }
     
     public static bool isDown()
     {
         return s_swipeDirection.Equals(ESwipeDirection.Down);
     }
     
     public static bool isLeft()
     {
         return s_swipeDirection.Equals(ESwipeDirection.Left);
     }
     
     public static bool isRight()
     {
         return s_swipeDirection.Equals(ESwipeDirection.Right);
     }
     
     public static bool isUpLeft()
     {
         return s_swipeDirection.Equals(ESwipeDirection.UpLeft);
     }
     
     public static bool isUpRight()
     {
         return s_swipeDirection.Equals(ESwipeDirection.UpRight);
     }
     
     public static bool isDownLeft()
     {
         return s_swipeDirection.Equals(ESwipeDirection.DownLeft);
     }
     
     public static bool isDownRight()
     {
         return s_swipeDirection.Equals(ESwipeDirection.DownRight);
     }
 }
 
               I assume this is a huge ask, but l have looked everywhere and can't find anything. Any help is appreciated. Thank you.
Answer by Nick4 · Aug 18, 2015 at 09:36 PM
Now that's a lot of code and I'm not going to read it all. But I understand what you're saying. Just a declare a variable that is type of boolean to control the player's input. Here's an example
 bool inputIsEnabled = true;
 
 public class MyCharacterControllerClass : MonoBehaviour {
 
     void Update() {
         if(inputIsEnabled){
             // Insert your SwipeController methods here.
         }
         if(/*Check if player is close to a wall using raycast*/) {
             inputIsEnabled = false;
         }
         else {
             inputIsEnabled = true;
         }
     }
 
 }
 
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Multi Touch Disable on Virtual Joy-pad (FPS) 0 Answers
Diagonal swipe issues on Android 1 Answer
Unity android bluetooth controller disconnect doesn't make array empty 1 Answer
Distribute terrain in zones 3 Answers