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 RobeeU · Dec 28, 2012 at 05:24 AM · c#physicsraycastraycasthit

Raycast problem

Hello!

I've got a problem while trying to get familiar with raycasting. I send a ray, and checks if its hit something, but when I try to get the hitpoint, it gives wrong point.

Code:

 RaycastHit hit;
 var mousePo = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y,Camera.main.nearClipPlane));
 Ray r = Camera.main.ScreenPointToRay(mousePo);
 Physics.Raycast(r, out hit);
 Debug.DrawRay(r.origin, hit.point, Color.green);
Comment
Add comment · Show 1
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 Fattie · Dec 28, 2012 at 07:07 AM 0
Share

did you try just

r = Camera.main.ScreenPointToRay(Input.mousePosition);

rather than using the clipping plane?

also it's often handy to include a time here,

Debug.DrawRay(r.origin, hit.point, Color.green, 5.0 );

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexfeature · Dec 28, 2012 at 07:04 AM

Hey Robee,

I think your logic is ok but there is a simpler way.

     var hit = new RaycastHit();
     var ray = yourCamera.ScreenPointToRay(Input.mousePosition);

     if(Physics.Raycast(ray, out hit)) 
     {
        Debug.DrawRay(ray.origin, hit.point, Color.green);
     }

Also, using Camera.main is tricky because if you have multiple cameras tagged MainCamera you will never be sure which one is returned by the property (it grabs the first one it finds). It's always better to assign your cameras explicitly either through a public field or at script initialization.

This will save you headaches and hours of debugging.

Hope this helps, Alex

Comment
Add comment · Show 8 · 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 RobeeU · Dec 28, 2012 at 10:04 AM 0
Share

http://imgur.com/VH48E , if you can see on the pic, the ray is not going where I draw it. The interesting thing, that the check is working. So if I'm not clicking on terrain, it doesnt show the ray. Im confused.

avatar image Fattie · Dec 28, 2012 at 10:10 AM 0
Share

(1) rob are you ABSOLUTELY SURE you have only ONE camera? Look carefully in Heirarchy. type in "cam" to search down through all. It is easy to accidentally dupe a camera.

avatar image RobeeU · Dec 28, 2012 at 10:12 AM 0
Share

yes, i'm sure. the ray's starting pos (the camera) is right, the end point, which is hit.point, is the wrong one, I think

EDIT: if i instantinate something at hit.point, it is okay. maybe there is something wrong with the debug.drawray

avatar image Fattie · Dec 28, 2012 at 10:18 AM 0
Share

add two lines of code

 Debug.DrawRay( hit.point, hit.point+Vector3(0,10,0), Color.white, 5 );
 Debug.DrawRay( hit.point, hit.point+Vector3(10,0,0), Color.red, 5 );

to clear things up. it's possible it's hitting something actually UNDER THE TERRAIN or some such. it's not possible to know what's actually going on, as yet.

Next. make a small cube 10cm on a side in the scene and make it bright yellow. Name it "yellow$$anonymous$$arker"

Add these lines of code also

 var newThingy = Instantiate( GameObject.Find("yellow$$anonymous$$arker");
 newThingy.position = hit.point;

Add those four lines .. and just Play and click a LOT !! :) Hope it helps

avatar image alexfeature · Dec 28, 2012 at 11:37 AM 0
Share

I think in order to be sure you are getting the hit you expect you should add a layer mask and only register hits on the terrain layer.

To do this you need to :

a. Assign the terrain to a layer (either an existing one or by adding your own). You do this by selecting the terrain object and then click on the layer button at the top right corner of the inspector (http://docs.unity3d.com/Documentation/Components/Layers.html);

b. you need to change this : if(Physics.Raycast(ray, out hit)) to this : if(Physics.Raycast(ray, out hit, 1 << layerNumber)) where layer number is the numeric value you can see in the layer editor next to your chosen layer.

Now the clicks you do will only be registered if they hit the terrain object. This should shed some light on whats going on.

PS: I'm not certain but it could be that the angle of the camera is messing with the clicks too. It's possible to have the terrain clip through the camera when you are on a steep slope or something . This will make your clicks land on the 'invisible' polygons of the terrain. $$anonymous$$aking them valid clicks but not where you expect them.

Like I said I'm not sure if collisions are registered on the backsides of mesh collider polygons.

Show more comments

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

11 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

Related Questions

What is wrong about this RaycastHit Event??? 1 Answer

Having a RaycastHit event create a component? 1 Answer

Trouble with checking Raycast 1 Answer

Get Component in RaycastHit game object 1 Answer

Make Ray hit own collider 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