- Home /
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);
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 );
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
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.
(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.
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
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
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.
Your answer
Follow this Question
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