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
0
Question by Bentoon · Jun 03, 2015 at 04:29 AM · c#iosraycasttouchitween

Raycast Hit not working

Hello

We made a script that toggles the camera between different iTween paths when you click objects on mobile

But the Recast isn't reliable with multiple buttons I even tagged the clickable objects

Here's script: Thanks

 public class ControlSampleRayA : MonoBehaviour {
     
     //NOTE: You have to TAG your Collider with "trigger_a"
     
     
     public int pathNo=0;
     public int path2Return2 =0;
     public float percentage=0f;
      float returnPercentage;
     public string debugLog="";
 
     public string trigger = "trigger_a"; 
     Camera camera;
     FlythroughCamera_Mod aController;
     public Collider coll;
     private RaycastHit hit;
     
 
     
     // Use this for initialization
     void Start () {
         camera = Camera.main;
         aController= camera.GetComponent<FlythroughCamera_Mod>();
         coll = GetComponent<Collider>();
         
     }
     
     
     void Update() {
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
             Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
             RaycastHit hit;
             if (coll.Raycast (ray, out hit, 100.0f)) {
                 if (hit.collider.gameObject.tag == trigger) {
                     if (aController.whichPath != pathNo){
                         returnPercentage=aController.percentage;
                         Debug.Log (debugLog+" "+ aController.whichPath+" "+ pathNo+" "+ returnPercentage);
 
                         aController.ToggleToPath (pathNo, newPercentage:percentage);
                     }
 
 
                     else
 
                     {
                         Debug.Log (debugLog+" "+ aController.whichPath+" "+ pathNo+ " "+ returnPercentage );
                         aController.ToggleToPath (path2Return2,newPercentage:returnPercentage);
 
                     }
 
                 }
             }
         }
     }
     
 }

Comment
Add comment · Show 6
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 Glurth · Jun 03, 2015 at 05:48 PM 1
Share

this form of Raycast: coll.Raycast (ray, out hit, 100.0f)

will detect a hit upon collider "coll", and no others. Since it looks like this collider does NOT actually belong to the objects you are clicking, you are never going to get a hit on those objects, unless by happenstance.

I think you want to use the physics.raycast version of this function to see if it has hit ANY of your clickable objects: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

avatar image Bentoon · Jun 03, 2015 at 06:56 PM 0
Share

Thanks Glurth!

but I define both the public variables "trigger" & "coll" in the inspector with each instance of the script, so that if the collider.gameObject.tag is such it should execute...?

Right?

  if (coll.Raycast (ray, out hit, 100.0f)) {
                  if (hit.collider.gameObject.tag == trigger)

  
avatar image Bentoon · Jun 03, 2015 at 08:42 PM 0
Share

I tried it with physics.raycast but it's not working for me.

I am testing it in Unity Remote and it works / but not in builds

Thanks

avatar image Glurth · Jun 03, 2015 at 09:41 PM 0
Share

but I define both the public variables "trigger" & "coll" in the inspector with each instance of the script, so that if the collider.gameObject.tag is such it should execute...?

We don't have enough information to answer that, yet. If you are creating a separate instance of the ControlSampleRayA class for each object you $$anonymous$$IGHT click on, then: YES

But I don't think that's what you are doing there... I think you have one of those classes defined for each select-able camera position. So, each ControlSampleRayA can have only one object's collider to checked against in your code. All the other clickable objects would be ignored.

If you use the collider.Raycast version then you $$anonymous$$UST compare it against every possible collider. Doing this is certainly feasable, and often more efficient than the physics raycast. But you need to keep you own list of colliders to check against. Note: this also eli$$anonymous$$ates the need for using & checking tags, since you can just consider only colliders on the list you check, are "tagged".

avatar image Bentoon · Jun 04, 2015 at 03:21 AM 0
Share

Thanks Glurth for taking the time.

Lets see if I can clarify this better, $$anonymous$$y camera is following a main path and along the way it passes a bunch of boxes each with a collider & also the ControlSampleRay script on it, if at anytime, you click on a distinct box the camera changes to another distinct path...

The other path is a loop and returns to the box, clicking on that box again returns it back to the main path

Since i have a bunch of colliders and I need these to each trigger specific paths I thought the coll.Raycast would be the better option...

When I try using physics.raycast it seems like anything I touch triggers path switches

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Help With Touch to Drag Script 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers

Detect the swipe and add force respective to it? 3 Answers

touch game objects on ios 1 Answer

Multiple Cars not working 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