Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 31, 2016 at 12:40 AM by maccabbe for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by matta9001 · Jan 30, 2016 at 04:41 PM · c#collisionraycastcollider

Raycast hit or miss in the same situation

I have a player controller and I am sending a ray from the origin of the camera. What I am trying to do is be able to look at a safe and if I press E, a dialogue pops up. But I have a really interesting problem that I have no idea how to fix. It will work sometimes and sometimes it will just not work. The really interesting thing is that even if I don't move the cameras position or rotation AT ALL, there is still no telling whether it will execute or not.

To demonstrate this, I shot a raycast 50 times from the exact same position.

http://i.imgur.com/9ivKTEb.png

Here are my results

http://i.imgur.com/u1mWJQ9.png

And finally here is my code

     void sendRay(){
         Camera camera = cam.gameObject.GetComponent<Camera>();
         Ray ray = cam.gameObject.GetComponent<Camera>().ScreenPointToRay(new Vector3(camera.pixelWidth * 0.5F, camera.pixelHeight * 0.5F, 0));
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit, 3)){
             print("hit");
             if(hit.transform.tag == "safe"){
                 safe.enter();    
             }
         }else{
             print("no hit");
         }
     }
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 Raresh · Jan 30, 2016 at 04:43 PM 0
Share

Did you make sure no colliders overlap? It happened to me once, was randomly flickering on/off.

avatar image Whiteleaf · Jan 30, 2016 at 05:31 PM 0
Share

You should try what Raresh said first, as it happened to me, too. Also, it's worth checking to see if it's tag is "safe". You also have to remember that tags are case sensitive. OR, you might have 2 game objects under an empty parent or something, say the door and the main safe part. It will hit those objects(if of course they have colliders on them) and then check if their tag is "safe", if it's not then it ignores it.

avatar image kubajs · Mar 22, 2020 at 12:04 AM 0
Share

I recommend you to use layer mask in the Physics.Raycast check. As Raresh says you're highly probably hitting another collider in between the camera and the target object. If you look downwards, you probably hit the player's collider.

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by matta9001 · Jan 30, 2016 at 05:54 PM

I had a very very weird problem, the rigidbody on my player was somehow affecting the child cameras position, and every frame it would change between two positions creating the effect of randomness, it took me a long time to solve but in the end what fixed it was selecting "Is Kinematic" in the rigidbody, yes I know it's very weird. Thanks for the help though!

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Rotated object raycasting in wrong directions!!? 3 Answers

Freeze rotation of just the box collider component 0 Answers

How to move an object on a terrain that will always stay on top of the terrain? 2 Answers

Restrict held object movement 0 Answers

Raycast doesn't interact with Tilemap Collider when in a 3D scene. 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