Raycast not detecting hit
For fast moving objects OnCollisionEnter doesn't work always. To solve this issue I thought raycast is best, but raycast is also not detecting hit.
if (Physics.Raycast(trans.position, trans.forward, out hit, 0.1f,)){
if(hit.transform != null){
Debug.Log("tag : "+hit.transform.tag);
}
}
This is not giving hit. Is there a solution to overcome this issue?
Note - I want the maxDistance = 0.1f. The object which is casting ray is not having any collider and the object is moving with a speed of 1000f.
rigidBody.AddForce(transform.forward * 1000f);
Have you drawn the raycast to make sure it's piintihg in the right direction?
Yeah i have checked using debug.drawline and the direction was right
Answer by Lukas-Brunner · Jan 12, 2016 at 01:15 PM
OnCollisionEnter() should work even for fast moving objects if you change the collosion detection in the rigidbody component to "continous". The only drawback should be the higher cpu cost.
If I hange the collosion detection in the rigidbody component to "continous" it is working fine. As you mention it has a drawback. So this method is not optimized.
well i guess the only other thing you could do, is to decrease the timestep of the physics engine, but this has the same effect on the cpu as my first suggestion. If your really cannot afford compromises and u are a real pro you could maybe write a compute shader and do the collision detection on the graphics card on your own. But don't ask me how ;) ...
Your suggestion about shader seems intereseting but I am not a pro. It will take some time to do that. For the time being I am using linecast. $$anonymous$$ay be I will use raycast. Not sure though.
Answer by UniqueMAX · Jan 12, 2016 at 12:23 PM
Here's my list of possible solution, hope it heps =)
Try playing your project frame-by-frame using the button on the right from Pause button in editor. Check if there's at least one frame where your ray actually goes through the object.
Try using the script without if(hit.transform != null). Physics.Raycast already returns a non-null value on hit. You can simply check hit's tag directly to know if it's the object you need.
if(hit.tag=="MyTag"){ Debug.Log(hit.tag); }
Are you sure you need such a short MaxDistance? Maybe your object is simply too far from the raycast source? Try this method: Raycast without MaxDistance and when it hits make it return the distance on hit.
Debug.Log(Vector3.Distance(trans.position,hit.transform.position));
Adding some other possible solutions:
In Project Setting/ Physics (2D or not) check the Layer Collision $$anonymous$$atrix, make sure the layers can hit each other (maybe this does nothing to do with a Raycast, but check any ways)
Try use the layer mays as -1 (everything) i notice there is a coma before the distance but no value, maybe the compiler is assu$$anonymous$$g 0 (nothing).
Use Gizmos to see if the distance is enough to a hit happen
Collision matrix is not a problem. I have checked that, When both are on default layers, still it is not working.
Tried this method as well. Same output.
I had already tried some of these methods.
Frame by frame I had already tried. The thing is when the force amount is 1000 it is not detecting anything. When the force is 100 hit is detecting 3 times.
Tried without if(hit.transform != null). Same output.
Yes I need such a short distance. Without maxdistance it is working fine. But with this it will detect before the collision and I don't want that.
Distance method option is one way of doing it. Still I feel this is not the right way.
Answer by thesleeve · Jan 12, 2016 at 03:40 PM
Are you sure that the objects are on layers that are set to interact with each other in the physics engine? If the objects are on separate layers, then you must click the corresponding check box on the collision matrix (Edit->Project Settings->Physics). See http://docs.unity3d.com/Manual/LayerBasedCollision.html for more details.
Yes the objects are on same layer. I have checked with default layer as well. The end result is the same.
Answer by Veerababu.g · Jan 13, 2016 at 07:05 AM
it's not working because of distance you are giving i guess.
do ray casting with infinity and check the distance. may it works in your case
Your answer
Follow this Question
Related Questions
How do you detect a mouse button click on a Game Object? C# 2 Answers
Adding collision to Raycast hit 0 Answers
Make raycast ignore hitbox? 0 Answers
How to freeze an GameObject on x axis in specific direction? 1 Answer
Why doesnt the value change? 2 Answers