Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by chelder · May 29, 2016 at 10:33 AM · raycastraycastingdebugdelegatedelegates

Physics.Raycast and Debug.DrawRay not working with Google Cardboard / Google VR? Or because within a Delegate?

I have a Physics Raycaster attached to the Camera. The Pointer Click Event Trigger is working correctly. However I need to do it from the source code. These are my attempts:

 private void SetOnPushButtonFireManager(){
     cardboard.OnTrigger += () => {
         Debug.Log("Button triggered!");
         RaycastHit hit;
         // if(Physics.Raycast(headGameObject.GetComponent<GvrHead>().Gaze, out hit, Mathf.Infinity)){
         if(Physics.Raycast(cameraGameObject.transform.position, cameraGameObject.transform.forward, out hit, Mathf.Infinity)){
               Debug.Log("Collision detected!");
         }
     };
 }

"Button triggered!" is shown in the Console. Unfortunately "Collision detected!" is not. However the Pointer Click Event Trigger is working correctly (the component attached in the inspector). How can I know what is going on? Why isn't it working?


Moreover, these are my attempts to see Debug.DrawRay working. I cannot see any line drawn:

         Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.green, 2, false);
         Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.yellow, 2, true);
         Debug.DrawRay(head.GetComponent<GvrHead>().Gaze.GetPoint(0), head.GetComponent<GvrHead>().Gaze.GetPoint(5), Color.red, 2, true);

         Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.green, 2, false);

         Ray ray = Camera.main.ViewportPointToRay (new Vector3(0.5f,0.5f,0f));
         Debug.DrawRay(ray.origin, ray.direction * 100, Color.white);
         Debug.DrawRay(ray.origin, ray.direction * 10);
         Debug.DrawRay(ray.origin, (ray.direction + new Vector3(0.1f,0.2f,0.3f)) * 10);

         Ray ray2 = cameraGameObject.GetComponent<Camera>().ViewportPointToRay (new Vector3(0.5f,0.5f,0f));
         Debug.DrawRay(ray2.origin, ray2.direction * 100, Color.white);
Comment
Add comment · Show 3
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 chelder · Jun 09, 2016 at 12:17 PM 0
Share

Debug.DrawRay was working well since the beginning. I did not see it because I was watching the Game view, and the ray is drawn in the Scene view!

No idea why Physics.Raycast returns false when it hits a target.

avatar image Mmmpies chelder · Jun 09, 2016 at 01:43 PM 1
Share

Not got access to Unity so I can't help much but use a LineRenderer to draw a line in the game view.

avatar image chelder Mmmpies · Jun 09, 2016 at 08:29 PM 0
Share

Thank you! That's good to know :)

2 Replies

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

Answer by chelder · Jun 09, 2016 at 10:03 PM

Debug.DrawRay was working well since the beginning. I did not see it because I was watching the Game view, and the ray is drawn in the Scene view! @Mmmpies also commented that we can use LineRenderer to draw a line in the Game view.

On the other hand, Physics.Raycast was not working because the target was an UI element. It seems Physics.Raycastonly works if the target has a collider attached.


So that is the first solution proposed. I used the Box Collider. Don't forget to correct the Size! Also, don't forget to add the Physics Raycaster script to the main camera. I used the following code and configured the Box Collider as follows:

 RaycastHit hit;
 if (Physics.Raycast (Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity)) {
         Debug.Log("Collider detected! 3D collider works!");
 }

screenshot Box Collider


The second solution proposed is dealing with the UI elements with its specific raycaster, UnityEngine.EventSystems.EventSystem.current. I only was able to make it work if the target has the Button and Raw Image components attached. The code and the screenshot follows:

 GameObject hit2 = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
 if (hit2 != null){
       Debug.Log("UI detected!");
 } 

screenshot button and raw image components


The third solution is maybe the best, who knows. We have to attached to our target a Box Collider 2D component. Then using Physics2D.Raycast. Don't forget to correct the size either. I set x and y to 200, and options unticked (sorry, this site does not allow me to add more than 2 attachments). Finally, we should add the Physics 2D Raycaster to the main camera. Here is the code:

 Ray ray3 = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
 RaycastHit2D hit3 = Physics2D.Raycast (ray3.origin, ray3.direction, Mathf.Infinity);
 if (hit3.collider != null) {
      Debug.Log("RaycastHit2D hit collision!");
 }

screen-shot-2016-06-09-at-234140.png (42.6 kB)
screen-shot-2016-06-09-at-231957.png (19.6 kB)
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
0

Answer by kainathanif28 · Nov 10, 2018 at 07:57 PM

hey, i want the same procedure for my game. i want that when main camera's reticle pointer hits the object, it will destroy. please help me from the start i'm bit confused. @chelder

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

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

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

Related Questions

My raycasting isn't working 1 Answer

Raycast not drawing where i tell it 1 Answer

Is there any way how to do raycasts like this? 0 Answers

Raycast on mouse position problem. how not to raycast to 0 1 Answer

Linecast ignores obstacles. 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