• 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
Question by jerryberry · Feb 12, 2014 at 05:43 PM · android2dmobiletouchdestroy

How to destroy object by touch?

I tried this code:

 if (Input.touchCount > 0)
     {
         var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
         var hit: RaycastHit;
         if (Physics.Raycast (ray, hit))
         {
             if (hit.collider.tag == "Enemy")
             {
                 Destroy(hit.transform.gameObject);
             }
         }
     
     }

But nothing happens when I play my game on the phone. I just want to destroy objects from prefabs when I touch them. They just fall down like raindrops. What should I do?

EDIT: Here's what works (Thank you JeffreyD!):

 if (Input.touchCount > 0)
 
     {
 
         var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
 
 
 
         if(hit.collider != null)
 
         {
 
             Destroy(hit.transform.gameObject);
 
         }
 
     }
Comment
DarkDra9on555
QQQ_QQQ

People who like this

2 Show 5
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 Romano · Feb 12, 2014 at 07:13 PM 0
Share

Does your game work in windows (or mac) if you swap touch for a mouse click?

avatar image jerryberry · Feb 12, 2014 at 07:41 PM 0
Share

Actually I don't know how to do it. Input.GetMouseButtonDown(0) have no position.

avatar image Romano · Feb 13, 2014 at 12:24 AM 0
Share

There can be problems if you have multiple overlapping colliders?

avatar image pablouwall · Apr 15, 2015 at 05:35 AM 0
Share

hi; raja your code is right but if i want to destroy clone's object ?

avatar image DarkDra9on555 · Nov 19, 2015 at 01:09 AM 0
Share

What did you attach this to?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by JeffreyD · Feb 12, 2014 at 06:22 PM

I'd do something like... bool myDebug = false; // set to true to see debug info in console void Update() { int fingerCount = 0; RaycastHit hitInfo; Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition); //Debug.DrawRay(transform.position, vfwd * 20, Color.red); foreach (Touch touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) fingerCount++; } if (fingerCount > 0) { if (myDebug) { Debug.Log ("FingerCount is : " + fingerCount); } if (Physics.Raycast(rayOrigin, out hitInfo, 20)) { if (myDebug) { Debug.Log("I see We hit ==> " + hitInfo.transform.name); } if(hitInfo.transform.tag == "Enemy") { if (myDebug) { Debug.Log("We hit the TARGET"); } Destroy(hitInfo.transform.gameObject); // can add time delay as second argument } } // END if raycast } // END if (fingerCount > 0) }

I've not tested this. Let me know if it works. Thanks. P.S. Link to touch info in docs http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html

Comment

People who like this

0 Show 17 · 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 jerryberry · Feb 12, 2014 at 06:53 PM 0
Share

But what is hit in Destroy(hit.gameObject);? I don't know what type this variable should be.

avatar image JeffreyD · Feb 12, 2014 at 09:30 PM 0
Share

Oops. Just fixed the code. Hit should be hitInfo. Like I said I did not test this. hitInfo contains the gameObject that is "hit" by the raycast, which is "shot" from your figure touch into the gamespace.

Oh, it should be from the finger touch not the mouse. Hmm, I'm using this code for rayCast on a game I'm running on my phone and it works. I'm guessing that the "Mouse" is your finger touch in this case. I'll have to look into it more into the difference between finger and mouse on Mobile. I'm still figuring out this stuff myself. I saw your post and figured I'd send what was working for me.

Did you try this (with the hitInfo fix)?

avatar image JeffreyD · Feb 12, 2014 at 09:43 PM 0
Share
 In the doc info on touch the 3rd example shows....
 
 
 for (var i = 0; i < Input.touchCount; ++i)
 { 
   if (Input.GetTouch(i).phase == TouchPhase.Began)
   {
     // Construct a ray from the current touch coordinates
     var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
 
   if (Physics.Raycast (ray))
   {
     // Create a particle if hit
      Instantiate (particle, transform.position, transform.rotation);
   }
 
 }
 
 

So, I need to update my own code away from mouse into touch. I'll let you know how it works out. In the mean time, this def of the raycast starting position as the mouse does work so it should be enough for you to try a test. Let me know how you make out.

avatar image jerryberry · Feb 13, 2014 at 04:14 PM 0
Share

'gameObject' is not a member of 'UnityEngine.RaycastHit'

avatar image JeffreyD · Feb 15, 2014 at 04:22 AM 0
Share

You are right. hitInfo.gameObject should be hitInfo.transform.gameObject as you have in your initial post. I fixed the code above.

Show more comments
avatar image

Answer by raja1250 · Jul 11, 2014 at 04:27 AM

assign this code to the main camera,and drag the game object to cubes variable.and tag the gameobject with "new" tag.Tested in android phone.

using UnityEngine; using System.Collections;

public class touch : MonoBehaviour {

 public GameObject cubes;

 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (Input.touchCount >0) 
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         if (Physics.Raycast(ray, out hit))
             if (hit.collider.gameObject.tag =="new")
         {
             Destroy(cubes);
             //cubes.rigidbody.AddForce(Vector3.forward * Time.deltaTime *500);
         
         }        
     }

} }

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Avoid touch input when pressing the "Pause" button 1 Answer

OnPointerExit Issue w/ Radial Menu on Android 1 Answer

Android input only works with remote 1 Answer

How to improve device heating (Android) 1 Answer

Why isn't this working? 3 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