• 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 hawken-2 · Jul 11, 2011 at 04:56 AM · iosraycasttap

detecting taps on 3d objects in iOS

Having trouble with this, and can't find a working example. I have 5 objects, that need to detect when they are tapped, this code seems to detect the taps if I place it on the objects, but picks up the tap anywhere on the screen, at the same time for all 5 objects! Any clues on how to just detect if the tap is on one object?

 private var hit : RaycastHit;
 private var ray : Ray;
 var titleCamera : Camera;
 
 function Update () {
 
 if(Input.touchCount == 1) {
         var touch: Touch = Input.touches[0];
         ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
         if(touch.phase == TouchPhase.Began && Physics.Raycast(ray.origin, ray.direction,hit)){
             //do something for this object
         }
 }

The 3D objects have rigidbodies and colliders.

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

6 Replies

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

Answer by Sarper-Soher · Jul 11, 2011 at 12:17 PM

You can add another set of conditionals to check if the hit object is the one you want. By the way do not add this script to the objects, add it to an empty game object. Each instance of this script sends a new ray so with one tap you are sending a lot of rays for that frame right now.

 switch(hit.collider.name){
     case "YourObjectName":
         //Do something
     break;
     case "AnotherObjectName":
         //Do something more
     break;
     //and so on...
 }
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
avatar image
-1

Answer by hawken-2 · Jul 12, 2011 at 04:21 AM

Ah I think I understand, so with iOS you need to have just one raycasting function and then detect which object it hit?

Seems a bit confusing because mouseUp raycasting and GUI taps/clicks don't work in this manner.

Anyone be so kind as to post a quick and dirty script they use for this?

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

Answer by hawken-2 · Jul 12, 2011 at 08:45 AM

heres the final code for those interested. Would still be interested in a bit of code that could be applied to individual objects though, tried out some other things, made sense in code but didn't work. Anyhow:

 var myCamera : Camera;
 var hit : RaycastHit;
 var ray : Ray;
 
 function Update () {
     if(Input.touchCount == 1) {
         var touch: Touch = Input.touches[0];
         ray = myCamera.ScreenPointToRay(Input.touches[0].position);
         if(touch.phase == TouchPhase.Ended && Physics.Raycast(ray.origin, ray.direction,hit)){
             
             switch(hit.collider.name){
                 case "object01":
                     Debug.Log("Object01 tapped");
                     //things
                 break;
                 case "object02":
                     Debug.Log("Object02 tapped");
                     //things
                 break;
             }
         }
         else if (touch.phase == TouchPhase.Ended && !Physics.Raycast(ray.origin, ray.direction,hit)) {
             Debug.Log("tap cancelled");
         }
     }
 }

this only works when the user lifts up their digit, sausage, whatever, without leaving the confines of that objects area, if they change their mind and slide their tap away, it reports the tap as cancelled.

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 MithosAnnar · Feb 06, 2012 at 01:40 PM 0
Share

Doesn't work, It says that the array index is out of range.

avatar image
0

Answer by Sarper-Soher · Jul 12, 2011 at 09:05 AM

Sure, create a new script named InputManager and fill it out as below. Then create a new Empty Game Object and name it InputManager just so you can find it later on in your scene and add this script to this GameObject.

 private var touchRay : Ray;
 private var touchHit : RaycastHit;
 
 var inputCamera : Camera;
 
 function Awake(){
     if(!inputCamera){
         inputCamera = Camera.main;
         Debug.Log("No camera defined for " + this + " . Main camera is being used now.");
     }
 }
 
 function Update(){
     UpdateTouchInput();
 }
 
 function UpdateTouchInput(){
     if(Input.touchCount == 1){
         var touch : Touch = Input.touches[0];
 
         if(touch.phase == TouchPhase.Began){
             touchRay = inputCamera.ScreenPointToRay(touch.position);
 
             if(Physics.Raycast(touchRay, touchHit)){
                 switch(touchHit.collider.name){
                     case "object1":
                         Debug.Log(touchHit.collider.name + " is hit.");
                     break;
                     case "object2":
                         Debug.Log(touchHit.collider.name + " is hit.");
                     break;
                     case "object3":
                         Debug.Log(touchHit.collider.name + " is hit.");
                     break;
                 }
         }
         }
     }
 }

BTW I haven't tested this so my apologies if there are any typos or syntax errors.

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

Answer by vengeance92 · Dec 08, 2011 at 12:45 PM

well I finally got it working, here is my final script (badly commented) I hope people can use it.

 var studioBlock : Transform; // blocker behind the door to stop you entering a room
 private var doorObject : GameObject; // the door gameobject (has a collider)
 var doorName = "stdr"; // name of the door gameobject
 private var Telefoon : GameObject;
 var telName = "ipod touch";
 private var hit : RaycastHit;
 private var ray : Ray;
 private var doorOpen : boolean = false;
 
 var inputCamera : Camera;
 
 
 function Start(){
 
 doorObject = GameObject.Find(doorName);
 Telefoon = GameObject.Find(telName);
     
 }
 
 function FixedUpdate (){
     if (Input.touchCount == 1) {
         var touch : Touch = Input.touches[0];
         
         if(touch.phase == TouchPhase.Began){
         print("tap");
         ray = inputCamera.ScreenPointToRay(touch.position);
         
             if(Physics.Raycast(ray, hit)){
                 switch(hit.collider.name){
                     case "stdr":
                         if(!doorOpen){
                             doorObject.transform.animation.Play();// plays the door animation
                             studioBlock.position.y = -100;// removes the room blocker
                             doorOpen = true;
                             //Destroy(this);// destroys the script so you can't spam the door
                         }
                         else
                         {
                             print("door is already open");
                         }
                     break;
                     case "scene phone":
                         print("iphone");
                     break;
                 }
             }
         }
     }
 }

it is attached to a empty game object somewhere in the scene

Dennis

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 schwertfisch · Aug 01, 2014 at 08:22 PM 0
Share

thanks, that was a big help after having read dozens of solutions to a similar problem of mine!

  • 1
  • 2
  • ›

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

touch game objects on ios 1 Answer

Why is the camera.screenpointtoray off? 1 Answer

Logic to detect objects entering between the player and camera? 1 Answer

jump at opposite angle of surface 2 Answers

Doubt about Raycast Performance 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges