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 GabboUnity · Oct 21, 2014 at 08:18 PM · detectiondetect

Problem with touch

Hi guys! I have a problem with the detecting of touch on the screen of mobile devices. I have a script for my videogame, but it doesn't work perfectly because when I touch the screen of my smartphone it detect more than one touch. I would that the game detect only one touch when I do it only one. I'm using (Input.touchcount > 0) How can I do? Thanks to all!

Comment
Add comment · Show 6
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 GabboUnity · Oct 21, 2014 at 06:05 PM 0
Share

Now? Is it correct?

avatar image GabboUnity · Oct 21, 2014 at 06:34 PM 0
Share

Now I wrote the type of touch that I have in my script..

avatar image GabboUnity · Oct 21, 2014 at 07:33 PM 0
Share
 #pragma strict
 var sphere : GameObject;
 var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
 function Update() {
 var hit : RaycastHit;
 if (Input.touchCount > 0) {
 if (Physics.Raycast(transform.position, Vector3.down, hit)) {
 Debug.Log(hit.collider.name +", " + hit.collider.tag);                       var rend = hit.collider.renderer;
 if (rend != null) {
 if (rend.material.color == renderer.material.color) {
 Debug.Log("Found a match");
 Scorecounter.Counter ++;    sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
  
 }
 }                                                     }
 }

avatar image GabboUnity · Oct 21, 2014 at 07:34 PM 0
Share

This is the script in which I use the checking of the touch..

avatar image Graham-Dunnett ♦♦ · Oct 21, 2014 at 08:20 PM 0
Share

Um, you're not using the touch position at all in your code.

Show more comments

1 Reply

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

Answer by zharik86 · Oct 22, 2014 at 06:28 AM

Your condition in your touch script works every frame. Of course, you detect more touch. Use correct the event touch. I change little your script:

  #pragma strict

  var sphere : GameObject;
  var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];

  function Update() {
   var hit : RaycastHit;
   if (Input.touchCount > 0) {
    //Check only for began event only first finger
    if (Input.GetTouch(0).phase  == TouchPhase.Beganj {
     if (Physics.Raycast(transform.position, Vector3.down, hit)) {
      Debug.Log(hit.collider.name +", " + hit.collider.tag);
      var rend = hit.collider.renderer;
      if (rend != null) {
       if (rend.material.color == renderer.material.color) {
        Debug.Log("Found a match");
        Scorecounter.Counter ++;
        sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
       }
      }        
     }
    }
   }
  }

I hope that it will help you.

Comment
Add comment · Show 11 · 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 GabboUnity · Oct 22, 2014 at 01:50 PM 0
Share

Beautiful! It's perfect for me! Thank you so much! @zharik86 But, I also would to detect when the player doesn't touch the screen in the correct moment. For example when the ball is rolling on the color of its same color..How can I do?

avatar image zharik86 · Oct 22, 2014 at 06:31 PM 0
Share

@GabboUnity I didn't understand about a correct time moment a little. But in your function Update() you already check coincidence of materials of a ball and surface under it, as I see from script.

avatar image GabboUnity · Oct 22, 2014 at 06:48 PM 0
Share

Yes, because the player must touch the screen while the ball is rolling on the plane that is colored like the sphere. I want that, if the player forget to touch the screen, the game stop. I hope that you can understand now.

avatar image zharik86 · Oct 23, 2014 at 07:21 AM 0
Share

@GabboUnity As I understood, check if colors of a ball and the plane match is necessary for you. And if the user managed to touch the screen. Then we will make this check. I hope that the script is attached to a ball:

  #pragma strict
 
  var sphere : GameObject; //What do variable?
  var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
  var my$$anonymous$$atch: boolean = false; //variable for match colors, our plane
  var myPlane: Transform = null; //variable for plane

  //Function for check match colors, return value true/false
  function CheckPlane(): boolean {
   var hit : RaycastHit;
   var tpCheck: boolean = false;
   if (Physics.Raycast(transform.position, Vector3.down, hit)) {
    //UPDATE2: use better hit.transform
    var rend = hit.transform.renderer;
    if (rend != null) {
     //UPDATE: add check variable match
     if ((rend.material.color == renderer.material.color || my$$anonymous$$atch) && myPlane != hit.transform) {
      Debug.Log("Found a match in check");
      myPlane = hit.transform;
      tpCheck = true;
     }
    }
   }
   return tpCheck;
  }
 
  function Update() {
   //Find change for plane every frame and check match/no match
   var tpCheck = this.CheckPlane();
   if (my$$anonymous$$atch) {
    if(tpChech) {
     //If you have match and function return true, thus you change plane: game over
     //GA$$anonymous$$E OVER - your function and etc
     //UPDATE2: for game over stop time
     Time.timeScale = 0;
    }
   } else {
    my$$anonymous$$atch = tpCheck;
   }

   var hit : RaycastHit;
   if (Input.touchCount > 0) {
   //Check only for began event only first finger
    if (Input.GetTouch(0).phase  == TouchPhase.Beganj {
     if (Physics.Raycast(transform.position, Vector3.down, hit)) {
      Debug.Log(hit.collider.name +", " + hit.collider.tag);
      //UPDATE2: use hit.transform
      var rend = hit.transform.renderer;
       if (rend != null) {
       if (rend.material.color == this.renderer.material.color) {
        Debug.Log("Found a match");
        Scorecounter.Counter ++; //your numerator?
        //UPDATE2: replace sphere.renderer on this.renderer and attach script at your ball
        this.renderer.material.color = colors[Random.Range(0, colors.Length)];
        my$$anonymous$$atch = false; //Touch is began and all check, than value is false
       }
      }  
     }
    }
   }
  }

I hope works.

avatar image GabboUnity · Oct 23, 2014 at 12:54 PM 0
Share

@zharik86 it's exactly what I would, but the only problem is that it stops the game at the second time that the ball rolls on the plane that has the same color and it doesn't stop it after the first plane (clearly if the player doesn't touch the screen)..

Show more comments

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

Detect If an oculus rift is attached or not 1 Answer

Need help to detect nearby / adjacent tiles / objects whatever, without collision... HELP,Need help to detect nearby / adjacent objects to make them available for further commands. 0 Answers

Auto-detect Arduino COM port 1 Answer

help with goal detection system,Goal detection systems not working 1 Answer

Strange Glitch Whenever I First Open Unity 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