Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 27, 2016 at 07:46 AM by rsamrry for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by rsamrry · Mar 23, 2016 at 05:52 AM · androidtouchmeshcollider

How can I detect touch on a Mesh Collider on an Android device ?

Hi, In my game, upon touching a mesh collider I want the game object of the mesh collider to get destroyed. I am using Unity 5.3.2 free version. I am attaching the code :

 void Update () {
     foreach (Touch t in Input.touches) {
         if (t.phase == TouchPhase.Began) {
             if (gameObject.GetComponent<Collider> ().Raycast (Camera.main.ScreenPointToRay (t.position), out hit, Mathf.Infinity)) {
                 Destroy (gameObject);
             }
         }
     }
 }


Comment
Add comment · Show 8
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 Abhiroop-Tandon · Mar 23, 2016 at 09:50 AM 0
Share

Try using tags on the object you want to destroy. Then check the raycast using the tag and then destroy the object.

avatar image rsamrry · Apr 18, 2016 at 03:29 PM 0
Share

Hi Guys, I tried to run a raycast and check for tag but am not able to destroy the object. please help.Here is the code. I looked at other discussions on the issue but they don't provide a clear cut answer. please note that my game object to be destroyed has a mesh collider and is not convex.

 void Update () {
             foreach (Touch t in Input.touches) {
                 if (Physics.Raycast (Camera.main.ScreenToWorldPoint (new Vector3(t.position.x, t.position.y, 1f)), Vector3.forward, out hit, $$anonymous$$athf.Infinity) && (hit.transform.tag == "sharkbody")) {
                     Destroy (hit.transform.gameObject);
                 }
             }
     }


Thank you in advance...@Abhiroop Tandon

avatar image Le-Pampelmuse rsamrry · Apr 19, 2016 at 12:02 PM 0
Share

I've converted this to a comment because it isn't an answer.

avatar image aditya · Apr 19, 2016 at 04:57 AM 0
Share

will you please tell us if your gameobject has a collider or not and if your gameobject has a tag sharkbody and not sharkBody or SharkBody, i m asking this because the answer of @Blue Cut is correct

avatar image rsamrry · Apr 19, 2016 at 07:10 AM 0
Share

@aditya my game object has a mesh collider(but it isn't convex), and it is tagged sharkbody. another thing...the game object to be destroyed is a child of another game object. does that make a difference? thanks

avatar image aditya rsamrry · Apr 19, 2016 at 10:22 AM 0
Share

no, Parenting doesn't make any difference ....

avatar image rsamrry · Apr 19, 2016 at 10:40 AM 0
Share

@aditya i can't see your full post. please add it as a comment and not a reply. thanks

avatar image aditya rsamrry · Apr 19, 2016 at 11:57 AM 0
Share

you are getting the full post, the 4 DOTS is the way how i end my comments ... and it will be great if you could send us some of your gameobject and code screenshots

3 Replies

  • Sort: 
avatar image
1

Answer by Blue-Cut · Apr 18, 2016 at 07:58 PM

Hello,

This code should works. (Note that is your not using multitouch, there is no reason to test all the touches, the index 0 will be fine)

 void Update()
 {
   if (Input.touchCount >= 1) 
   {
      // The pos of the touch on the screen
      Vector2 vTouchPos = Input.GetTouch(0).position;
         
      // The ray to the touched object in the world
      Ray ray = Camera.main.ScreenPointToRay(vTouchPos);
         
      // Your raycast handling
      RaycastHit vHit;
      if(Physics.Raycast(ray.origin,ray.direction, out vHit))
      {
          if(vhit.transform.tag == "sharkBody") 
          {
             Destroy (vHit.collider.gameObject);
          }
      }
  }
 }

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 rsamrry · Apr 18, 2016 at 10:22 PM 0
Share

Hi, Sorry, your code didn't work.

I have multiple touches, so i modified it a little.

 for (int i = 0; i < Input.touchCount; i++) 
             {
                 // The pos of the touch on the screen
                 vTouchPos = Input.GetTouch(i).position;
 
                 // The ray to the touched object in the world
                 ray = Camera.main.ScreenPointToRay(vTouchPos);
 
                 // Your raycast handling
                 if(Physics.Raycast(ray.origin,ray.direction, out hit))
                 {
                     if(hit.transform.tag == "sharkbody") 
                     {
                         Destroy (hit.collider.gameObject);
                     }
                 }
             }

I declared all the variables on top, so that's not the problem.

thanks...@Blue Cut

avatar image Blue-Cut rsamrry · Apr 19, 2016 at 07:34 AM 0
Share

The code I gave to you is not coded in blind, it's from a working case. So I am more than sure it's working. As @aditya said, the typo is important for the tag. Can you try to put some Debug.Log at each step of the function, to check that all is happening as you imagine.

For example in the if of the raycast, check that the collided object is the good one :

 Debug.Log (hit.transform.name);
avatar image
0

Answer by Waka-Takaki · Apr 18, 2016 at 04:42 PM

Have you tried this:

 RaycastHit hit;
 
 void Update () {
     foreach (Touch t in Input.touches) {
         if (Physics.Raycast (Camera.main.ScreenToWorldPoint (new Vector3(t.position.x, t.position.y, 1f)), Vector3.forward, out hit, Mathf.Infinity)) {
             if(hit.transform.tag == "sharkBody") {
                 Destroy (hit.transform.gameObject);
             }
         }
     }
 }
Comment
Add comment · Show 1 · 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 rsamrry · Apr 18, 2016 at 09:46 PM 0
Share

Hi, Thanks for the reply but i don't see how what you said is logically different from what i have tried. bye.

avatar image
0

Answer by rsamrry · Apr 24, 2016 at 02:20 AM

Thanks everybody for their contributions. The answer given by @Blue Cut is right. The problem was that my game object's collider was a non-convex mesh collider and they don't respond to raycasts. I simply replaced that collider with a sphere collider and it worked. thanks again.

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

Follow this Question

Answers Answers and Comments

65 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

buttons vs touch. 1 Answer

Latency with android touch 1 Answer

Spawning enemies after death help 0 Answers

Non-responsive touch controls after showing ads. 0 Answers

Clicker Attack 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