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
2
Question by CupOfMayo · Aug 16, 2013 at 02:25 PM · javascripttouch

Destroying object by touch on Android? (JS)

Hey so, let's say I have got 5 cubes with same script attached and with random positions and i want to destroy them (not every at once) while tap on it. I found on google someone talking about Physic.raycast but I don't really understand how does that work :/ It would be awesome if someoce can maybe explain that to me :)

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
4
Best Answer

Answer by Joyrider · Aug 16, 2013 at 02:29 PM

Have a look at the scripting reference, it has examples and everything

Basically, your create a ray from you tap's position, in the same direction the camera is facing. And you'll detect if the ray hits any colliders. Which simply means you create a ray under your finger. (For this all your cubes need to have a collider object!).

if he does hit, you can get the RayCastHit data back from the raycast, and access severl types of data from that hit (exact point, object, texture uv,...)

Comment
Add comment · Show 4 · 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 CupOfMayo · Aug 16, 2013 at 02:50 PM 0
Share

Oh okay I see :) But i'm having weird error... I was trying to use and example from scripting reference:

     function Update() {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, 100)) {
             print ("Hit something");
         }
     }

But When I attach it to camera and I run it I have his:

 NullReferenceException
     UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:376)
     Raycast.Update () (at Assets/Scripts/Raycast.js:2)

Should I attach it to another object, or am I doing wrong something else?

avatar image CupOfMayo · Aug 16, 2013 at 03:11 PM 0
Share

Allright i fixed that, i just had to change tag of the camera to $$anonymous$$ainCamera :)

avatar image CupOfMayo · Aug 16, 2013 at 04:06 PM 3
Share

Finally got code working :D Here is it if someone is interesed:

     function Update ()
     {
       if (Input.touchCount > 0)
       {
           var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
           var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) 
       {
               Debug.Log(" You just hit " + hit.collider.gameObject.name);
        if(hit.collider.tag == "Part")
        {
         Destroy(hit.transform.gameObject);
        } 
       }
      }
     }
avatar image shadows_s · Oct 24, 2014 at 11:47 PM 0
Share

touches works perfect) *ps. I used it for my mobile UI, as I know standart OnGUI is bad for mobiles.

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

16 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Android touch gui help!!! 2 Answers

How to drag a GameObject with touch but at a limited speed 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