- Home /
Raycast with strange Behaviour
hi,
i have a question about using raycasts for collision detection. first of all i will describe the application environment. u can imagine a 3d dna with two strands and some base pairs. the helix (parent of these pairs) is rotating around its local x-axis. only the pairs are providing colliders. the user should be able to use the mouse to interact with the pairs.
now the problem. like described above i use a raycast in the update method for detecting collision. most of the time, the result is correctly (collision is detected and the right pair is selected). but sometimes, although the ray is obviously right, the recognition isn't working.. also possible but rare is the fact that if the collision is detected, a wrong pair is selected.
what am i doing (similar code):
Transform hittedPair;
Dictionary<Transform, int> pairs;
Ray ray;
RaycastHit hit;
Camera cam;
void Update() {
if(hittedPair == null && Input.GetMouseButtonDown(0))
{
ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
hittedPair = hit.collider....;
// So pairs[hittedPair] returns index (0, 1, 2..., n)
}
}
else if(hittedPair != null && Input.GetMouseButton(0))
{
// Move HittedPair with Mouse
}
else if(hittedPair != null && Input.GetMouseButtonUp(0))
{
// Do something
hittedPair = null;
}
}
in short:
mouse clicks that should obvious be recognized as collision are not!
example: i click on pair with index 1 and move.. pair with index 4 is selected and moved
i don't know what is happening there.. so maybe someone has a helpful suggestion for me.
I see nothing wrong in this code. You need to add some Debug.Log() statements to figure out if 1) you are hitting something you don't expect, or if the Raycast() is failing. Then you need to isolate a specific example of failure and exa$$anonymous$$e what is going on.
if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity))
{
Debug.Log(hit.collider.name+", "+hit.collider.tag);
hittedPair = hit.collider....;
// So pairs[hittedPair] returns index (0, 1, 2..., n)
}
else
{
Debug.Log("Raycast failed");
}
this is what i already have done. a lot of outputs. and its point two. unfortunately if i click on pair with id 1, sometimes another pair (e.g. index 7) will move. and also the output says "index 7". the debug.drawray shows the correct line, too... if i want to click on a pair and there is obviously a collision, sometimes it won't be recognized (and the debugger says "raycast fail".. but why ?
in addition: this is the almost the original code which is used in my application. so there isn't any relevant logic missing
The only guess I have that matches your observations is a misalignment of the collider for the objects with the underlying mesh. So check that first.
if i click on pair with id 1, sometimes another pair (e.g. index 7) will move
Does that mean the Debug.Log() reports 1 and then 7 moves? If so, that points to an issue elsewhere in your code. That is, if the object you click on is 1 and reports as 1, then it is not the Raycast() that is failing.
not exactly. if i click on 1, it is possible that maybe index 7 is choosen. the mouse is on pair 1, but debugger says seven. and if i move then, pair 7 will move parallel to my mouse. for this problem i thought, maybe it is because of the used collection and the equality.. but don't know. the other problem is, that if i click exactly on the pair, sometimes it will detected as "non-collision" (also debugger output). if i try another mousedown event a few ms later, then it maybe works. it is unpredictable, but 80-85 percent of events is working fine.
addition: i found out, that the missing collision detection is caused by the rotation of the parent (helix). so moved the animation code into the FixedUpdate() method. still not working. any suggestions ?