• 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

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

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

Toggle collider with key 0 Answers

How to get contact points between a mesh collider and a line? 1 Answer

Set variable to position of collided object. 0 Answers

create object on raycast collision that follows raycast. 0 Answers

How do I use the capsule Collider? 1 Answer

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