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 SS2095 · Jun 16, 2014 at 07:24 PM · androidtouchupdate

detecting touch android just once in update

i'm making a whac a mole game and i want the moles to detect a hit only once in a lifetime of 1 second,and if not hit to reduce the number of lives the player has.unfortunately the script is called too many times the first time itself and the user dies even before three moles show up .the deadcount reaches zero in the first mole itself.here's the code

 using UnityEngine;
 using System.Collections;
 
 public class MoleHit : MonoBehaviour {
 
     // Use this for initialization
     public Vector2 touchpos;
     public static bool hitFound=false;
     public Sprite cracked;
     public Sprite intact;
     private SpriteRenderer sp;
     void Start () {
                 sp = gameObject.GetComponent<SpriteRenderer> ();
                 sp.sprite = intact;
         }
     void FixedUpdate()
     {
 
 
         if(Input.touchCount==1) {
                         if (Input.GetTouch (0).phase == TouchPhase.Began) {
                                 Vector3 pos = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
                                 touchpos = new Vector2 (pos.x, pos.y);
                         }
         
                         if (collider2D.OverlapPoint (touchpos)) {
                                 sp.sprite = cracked;
                                 hitFound = true;
                                 audio.Play ();
                         }
                 }
         Debug.Log (hitFound);
         Scorer ();
 
     
     }
     
 
     void Scorer()
     {
         if (hitFound) {
 
             ScoreCounter.countScore++;
             hitFound=false;
 
                 }
         else {
             ScoreCounter.deadCount--;
                 }
 
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by pstreef · Jun 16, 2014 at 08:28 PM

I would add a timer and use regular update. AFAIK using fixed update is not recommended when using input.

It would something like this: (untested!)

 using UnityEngine;
 using System.Collections;
  
 public class MoleHit : MonoBehaviour {
  
     // Use this for initialization
     public Vector2 touchpos;
     public static bool hitFound=false;
     public Sprite cracked;
     public Sprite intact;
     private SpriteRenderer sp;
     public float TimeoutTime = 1.0f;
     private float timeCounter;
     private bool hitSet;
     void Start () {
                 sp = gameObject.GetComponent<SpriteRenderer> ();
                 sp.sprite = intact;
                 touchpos = new Vector2(0.0f,0.0f);
                 timeCounter = 0.0f;
                 hitSet = false;
         }
     void Update()
     {
         timeCounter += Time.DeltaTime;
         if(Input.touchCount==1 && !hitSet) 
         {
             hitSet = true;
             if (Input.GetTouch (0).phase == TouchPhase.Began) 
             {
                 Vector3 pos = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
                 touchpos = new Vector2 (pos.x, pos.y);
             }
         }
         
         if(timeCounter > TimeoutTime)
         {
             timeCounter = 0.0f;
             hitSet = false;
             if (collider2D.OverlapPoint (touchpos)) 
             {
                 sp.sprite = cracked;
                 hitFound = true;
                 audio.Play ();
             }
             Debug.Log (hitFound);
             Scorer ();
         }
     }
  
  
     void Scorer()
     {
         if (hitFound) {
  
             ScoreCounter.countScore++;
             hitFound=false;
  
                 }
         else {
             ScoreCounter.deadCount--;
                 }
  
     }
 }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

touch buttons function OnGUI issue 1 Answer

How to touch, move and rotate 3D objects in XZ? 1 Answer

If you help me, i make you 3d object 1 Answer

multi-touch woe, index out of bounds? 1 Answer

How to make a android touch menu? 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