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
1
Question by 1337GameDev · Mar 15, 2012 at 10:57 PM · mobiletouchtime

Long press stationary

How would i do a long press?

My algorithm i am trying to do goes like this (but doesnt seem to work)

if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began) {touch0BeginTime = Time.time;}

if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary && (Time.time - touch0BeginTime)) {//long press code here}

Why doesn't this work?

Comment
Add comment · Show 2
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 1337GameDev · Mar 15, 2012 at 11:23 PM 0
Share

I've ran into another odd problem, whenever i put guiText.text = "random text";, the entire iff block seems to be skipped over. Without that code, it works fine. Why is this? guiText is a guiText object that works fine for other methods.

What could make code be skipped over?

avatar image 1337GameDev · May 04, 2012 at 02:18 PM 0
Share

The code was skipped because of a null reference. It irrationally skips code due to null references similar to segmentation faults in c++

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by deepthinker · May 04, 2012 at 08:15 AM

Try this:

 private var longPressDetected: boolean = false;
 private var newTouch: boolean = false;
 private var touchTime: float;
 
 function Start () {
 
 }
 
 function Update () {
     if (Input.touchCount == 1){    
         var touch: Touch = Input.GetTouch(0);
         if(touch.phase==TouchPhase.Began){
             newTouch = true;
             touchTime = Time.time;
             Debug.Log('touchTime='+touchTime);
         }else if (touch.phase == TouchPhase.Stationary){
             if(newTouch==true && Time.time-touchTime>1){
                 Debug.Log('longpress detected');
                 newTouch = false;
                 longPressDetected = true;
             }else if (newTouch == false && longPressDetected==false){ // began not detected
                 newTouch = true;
                 touchTime = Time.time;
                 Debug.Log('touchTime='+touchTime);
             }
         }else{
             newTouch = false;
             longPressDetected = false;
             Debug.Log('setting newTouch false');
         }
     }    
 }
Comment
Add comment · 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
1

Answer by DaveA · Mar 16, 2012 at 12:03 AM

(Time.time - touch0BeginTime) will be non-zero, hence 'true' in the next frame. You need to compare it to some real amount of time, like

var longPressTime = 2.0;

(Time.time - touch0BeginTime) > longPressTime

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 1337GameDev · Mar 16, 2012 at 12:17 AM 0
Share

sorry i forgot to type that, i have it in my code. just thought too quickly. Why would it skip my code though? And is there an order to using touchphases and such? Like if i test if touchphase == end before touchphase.began?

avatar image 1337GameDev · May 04, 2012 at 02:22 PM 0
Share

Thanks for the answers but i found a way to do it. It's similar to "timidguy" but i did checks in one line and had code to check my touch phases in order (doing them out of order seems to make unity skip execution of code between the out of order checks. And example is checking if TouchPhase.Began after checking TouchPhase.$$anonymous$$oved. It's odd but unity hates it.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with coding a long press touch c# 0 Answers

Changing keyboard input to touch in script 0 Answers

What happens to Input.mousePosition on mobile when there are no touches? 0 Answers

Unity 2D Mobile Game Drawing Mechanic 0 Answers

How do I detect where touches are (Mobile) 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