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 Lunneth · Jun 25, 2013 at 03:54 AM · inputtouchtouchestouchcount

Problem with Input.touchCount

I have a problem with Input.touchCount changing to 0 before I release my finger from the screen. I'll hold my finger on one of my move spots on the screen, and the transform will move. After about ~15 seconds Input.touchCount will change back to 0 (with my finger still in the same position) causing my moveVector to go to (0,0,0). I will stop moving at this time, and I don't want that. I can't seem to find if touches have a expiration time, which might be what is causing this. Any help would be appreciated. Here is my code for moving.

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour {
     
     Vector3 moveVector = new Vector3(0,0,0); //Move Vector is added to my transform every frame
     Vector2 touchVector = new Vector2(0,0); //Where the touch is on the screen
     public float moveSpeed = 1.0f; 
     private float screenWidth;
     private float screenHeight;
     private Transform myTransform;
     
     // Use this for initialization
     void Start () {
         screenWidth = Screen.width;
         screenHeight = Screen.height;
         myTransform = transform;
     }
     
     // Update is called once per frame
     void Update () {
         DetectTouches();
         transform.position += moveVector * Time.deltaTime;
         myTransform = transform;
     }
     
     public void DetectTouches(){
         if(Input.touchCount == 0){
             moveVector = new Vector3(0,0,0);
         }
         else if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began){
             touchVector = Input.GetTouch(0).position; //Just used to display on the GUI for debuging
             
             //Move to the left
             if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 120 && 
                 Input.GetTouch(0).position.y >= 75 && Input.GetTouch(0).position.y <= 175){
                     moveVector.x = -moveSpeed;    
                     //transform.Translate(-moveSpeed * Time.deltaTime, 0 , 0);
             }
             
             //Move to the right
             else if(Input.GetTouch(0).position.x >= 125 && Input.GetTouch(0).position.x <= 225 && 
                 Input.GetTouch(0).position.y >= 75 && Input.GetTouch(0).position.y <= 175){
                     moveVector.x = moveSpeed;
                     //transform.Translate(moveSpeed * Time.deltaTime, 0 , 0);
             }
             
             //Move to the front
             else if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 225 && 
                 Input.GetTouch(0).position.y >= 180 && Input.GetTouch(0).position.y <= 230){
                     moveVector.z = moveSpeed;    
                     //transform.Translate(0, 0 , moveSpeed * Time.deltaTime);
             }
             
             //Move to the back
             else if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 225 && 
                 Input.GetTouch(0).position.y >= 20 && Input.GetTouch(0).position.y <= 70){
                     moveVector.z = -moveSpeed;    
                     //transform.Translate(0, 0 , -moveSpeed * Time.deltaTime);
             }
             
         }
         //else if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended){
             //moveVector = new Vector3(0,0,0);    
         //}
     }
     
     void OnGUI(){
         GUI.Box(new Rect(20, screenHeight - 175, 100, 100), "<");
         GUI.Box(new Rect(125, screenHeight - 175, 100, 100), ">");
         GUI.Box(new Rect(20, screenHeight - 230, 205, 50), "^");
         GUI.Box(new Rect(20, screenHeight - 70, 205, 50), "B");
         
         //Next is just used to help debug
         GUI.Label(new Rect(10, 10, 500, 25), touchVector.ToString() + " Transform: " + myTransform.position + " MoveVector: " + moveVector + " " + Input.touchCount);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

What is the difference between iPhoneInput & Input ?? 1 Answer

Why can't I press both my touch controls at the same time? 1 Answer

Infuriating bug: Android build occasionally dropping touch input. 0 Answers

Input.touchCount won't return 2 or more on iOS. 1 Answer

Input.touches vs Input.GetTouch 1 Answer


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