0
votes

I am trying to make some kind of simulation program with Unity. The simulation includes a missile launched from an aircraft and a terrain. I get the coordinate data required for the movement of the missile from another program using a socket connection. I created an explosion effect for the missile to explode as soon as the missile and the terrain collided. But the explosion effect is not triggered in any way.

I used the OnCollisionEnter() method to detect the collision, but this method does not seem to work.

enter image description here

The missile has its own rigidbody and collider and The terrain has Terrain Collider, but still no collision is detected and the missile passes through the terrain.

What could be the cause of this error?

EDIT :

I thank everyone for their help, but none of the solutions worked. I solved the error using the OnTriggerEnter method. For this, I also had to enlarge the object's collider a little more.

1
I'm assuming that you have colliders on both the missile and the terrain. What collision detection are you using for the missile? Also, check this doco: docs.unity3d.com/Manual/ContinuousCollisionDetection.html - HumanWrites
I tried both "Continious" and "Continious Speculative" methods as the collision detection setting for the missile, but the result did not change. The collision is not detected by Unity. - emrcnort
I noticed that the collision is detected and the explosion effect is triggered when the missile is not moving and the terrain is manually moved through the Scene window and touches the missile. I use the Vector3.MoveTowards() method for the movement of the missile. Could this be the cause of the error? - emrcnort
You can use Vector3.MoveTowards() but since you're using a Rigidbody it should be used as an argument passed to the rigidbody's MovePosition method i.e rb.MovePosition(Vector3.MoveTowards(...)) and that should be called in FixedUpdate. - HumanWrites
@emrcnort Could this be the cause of the error yes if you are doing this in Update or in general directly on the transform without passing it through the Rigidbody as metnioned by HumanWrites you might already have passed the colliders before Unities Physics get to check for collisions - derHugo

1 Answers

1
votes

As mentioned in the comment section above (I dont have enough rep to contribute to the discussion but will provide a solution), you need to enable continuous collision detection. Once you have done that we can move onto the next step.

It is generally bad practice to interact with rigidbodies in update and it can lead to all sorts of strange bugs so I wouldn't recommend it. That being said I dont think it's the cause of your issues. As @HumanWrites mentioned, you are manually moving the position each frame which causes your missile to never actually collide with your mesh. the solution to this is:

rb.MovePosition(Vector3.MoveTowards(...))

The reason for this is that by using the method on the rigidbody, you inform the physics engine that you want to move from one position to the other and you would like the physics to take care of this instead of you doing it directly. This allows it to "sweep" between the current position and target position and detect any collisions that would have happened along the way.

Your missile is moving too fast to ever actually touch the mesh within a frame so you need to rely on the physics engine doing that sweep check.