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
1
Question by jpac · Aug 23, 2012 at 09:10 AM · raycasttouches

onTouchEvent -like happenings in Unity3D

I'm trying to achieve something like onTouch event in Unity3D.

Basically what I want to do is trigger an event whenever a particular object is touched.

I tried using Input.touches but it also listens to touch events outside the object. How can I achieve this?

Comment
Add comment · Show 1
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 Fattie · Aug 23, 2012 at 10:00 AM 0
Share

added some example code to get you started

1 Reply

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

Answer by Fattie · Aug 23, 2012 at 09:17 AM

I'll tell you !

  1. buy an iPad because it's happier than the google system :-)

  2. you get the user's touch of their grubby finger on the glass. say it is at "355,234" just for example.

  3. you RAYCAST from that point "355,234" in to the scene through the camera!

  4. the RAYCAST might hit something. if so, which object is it? often it will just the ground, clouds, fat tourists standing around on beach, whatever

  5. is your Particular Object touched? you're done!

Hope it helps!

There is a massive amount of advanced code on the topic here http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html

Note that, somewhat confusingly, you can either do this basically "on the object itself" (using collider.Raycast) or anywhere else (using Physics.Raycast)

Here's a general example

 function Update()
     {
     // hello world example starter code for the consumer "touching"
     // the object to which this script is attached.
     for ( t = 0; t < Input.touches.Length;  ++t )
         if ( Input.touches[t].phase == TouchPhase.Began )
             {
             if ( collider.Raycast(
                     theCam.ScreenPointToRay(
                         Input.touches[t].position),
                     theHit,
                     83 )
                 )
                 _IGotTouched!!!();
             }
     }
 private var theCam:Camera;
 private var theHit : RaycastHit;
 function Awake()
     {
     theCam = GameObject.Find("gameCam").GetComponent(Camera);
     }
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 jpac · Aug 24, 2012 at 12:57 AM 0
Share

so it did appear.. haha :)) shame on me for such a lame question.. hehe, anyway, I did see somewhere the use of raycast but not really sure what it really is for, thanks for the clarification sir!)

I believe there is a component/object in Unity3D named RayCastHit, right? so I just have to use it and attach it to the object I want to be touched, am I getting this correct sir?

e.g., I want a sphere object to be able to receive touch events, I'd add a RayCastHit component there..

thanks :)

avatar image Fattie · Aug 24, 2012 at 06:19 AM 0
Share

ok all you have to do is put the code given, in a script and attach that script to the sphere.

that script would be called a "component". you would be attaching it to the game object in question.

try it out, it will work perfectly!

You actually have to "do" a raycast. In fact the line of code "collider.Raycast..." is literally casting a ray for you.

Cheers

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer

Avoid button touches to be registered as swipe. 1 Answer

Ray error with touches.position 1 Answer

Raycast causes game to crash when shooting up 1 Answer

Cutting a Sprite 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