• 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
Question by CameronARP · Aug 18, 2015 at 09:07 PM · c#androidcontrollerdisableswipe

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.

Comment
IgorAherne

People who like this

1 Show 0
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

Answer by Rioneer · 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;
         }
     }
 
 }
 
Comment
IgorAherne

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Multi Touch Disable on Virtual Joy-pad (FPS) 0 Answers

Unity android bluetooth controller disconnect doesn't make array empty 1 Answer

Distribute terrain in zones 3 Answers

How to detect on android if bluetooth controller disconnects? 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