Understanding what causes collision events - unity3d

https://docs.unity3d.com/2021.1/Documentation/Manual/CollidersOverview.html
While getting some basic collision detection set up I referenced the official docs on the matter. Unfortunately, this left me more confused than when I started.
I made a simple scene. In this scene I added a sphere with a sphere collider (non-trigger) and a Kinematic Rigidbody. A script moves this object on the X axis when played.
I then created cube with a box collider (non-trigger). This object has no rigidbody or scripts attached.
Looking at the chart in the documentation linked to above, this should fall under the Static Collider and Kinematic Rigidbody Collider interaction, which should not create collision events.
However, if I add a OnCollisionEnter() to the sphere, it will call this method when the objects collide. Without physics they both continue on their way, otherwise oblivious to each other.
My concern then is that either the documentation isn't entirely correct or I have a fundamental misunderstanding of what it is telling me.
What's going on here?

Related

Unity Tilemap 2D player getting stuck in a platform

the question is quite simple. I have this platform and I do not want the player to collide with it when it jumps on the side or under it. In my Tilemap I have already added a composite collider 2D to the collider, so it doesn't separate each tile into a different collider.
As you can see, there are 2 tilesets but 1 collider thanks to the composite collider 2D. I have also changed the physics of this tileset, which is why the collider is so small (which is intended) so that the player can walk under it with no issues: Ej
However, when I jump to try to reach it by side, it does collide, ignoring the side arc function. I have never used a side arc, so I'm not sure if it should be done like this.
This is what happens instead of going through the platform it collides with it ... I thought Side Arc would let me go through the platform if I go by the sides, which is what the logic of the statement of a side arc would do but I guess I was wrong.
it gets stuck instead of going through, so what am I missing or doing wrong ?
I know I could simply hardcode it by making it trigger and when the "ground" collider attached to my pj touch it remove the trigger but I wanted to know if it was possible without that.

The problem with the correct use of a rigid body in a 2D game

I decided to make a 2D game for the PC on Unity, where the character moves around the stage. But I have a problem with the correct use of Rigid Body 2D.
In this picture you can see my playing field.
https://i.stack.imgur.com/PBhwx.png
I want my character to move around the entire field and when colliding with objects does not go through them.
My field is Tilemap, but I hung the Box Collider on the Grid. Boxing collider surrounds the grid.
But when I use Rigid Body and turn on the game, the object falls through. And if you remove the Rigid Body, then it just goes through objects.
I tried to use different types of Rigid Body, when using kinematic and static, sprite goes through houses.
The way it works is when you attach a RigidBody2D to an object, you give it physics properties, but it won't do anything useful without a collider. If you have an object with collider attached - every other object with collider attached will collide with it.
In your case the player should have RigidBody2D and BoxCollider2D. The ground should have BoxCollider2D, and basically everything you want the player to collide with should have BoxCollider2D. So the houses should have BoxCollider2D as well.
This setup should allow the player to walk on the ground without falling through it, and collider with houses.
Make sure that the collider's area is correct. You can preview it in the editor and edit it if it's not correct.
Also if you want it to work make sure the camera points down and your game setup is alongside Y axis so if there's a rigid body object falling down it falls down on the ground not before/behind it.

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

The GameObject passes through the terrain - Unity3D

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.
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.
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.

A collider is better to be static when checking collision?

So... As far as I know it's only good practice to make any GameObject Static if that object is not meant to move, therefore I also thought even a rolling stone with a rigidbody shouldn't be a static GameObject.
However, I was commented by someone as a 'Unity Tip FYI' the physics calculation spikes up when an object checks collision whilst it's rotating, so to make the collider static for the sake of better performance(even meer small amount).
I'm really not getting this idea.
Does this make any sense to anyone here??
Not exactly sure what that comment is saying. It doesn't make sense. Also, it looks like you are confusing static collider and marking GameObject as static.
Marking Object as static from the Editor
Telling Unity to batch it. It is used to increase rendering performance. (This has nothing to do with colliders)
Static Collider
Telling Unity that the Rigidbody will not be moved.
From your question, you are describing Static Collider. Static Collider work for both 3D and 2D physics system.
What is a Static Collider:
Static Collider is when you have a GameObject with any type of collider but no Rigidbody attached to it.
How to create a Static Collider:
To make a GameObject a Static Collider on, attach a collider to that GameObject but don't attach Rigidbody to it. It is now a Static Collider.
To make it non Static Collider, attach Rigidbody to it.
What it is used for:
When you have many Objects that you want your collide with but you are 100 percent sure that this Object will not be moved by you or player.
One good example is a brick wall. You simply want the wall to stop the player but you don't want to move the wall. It could also be used for stones in an environment to prevent player collison. Imagine having to attach hundreds or thousands of Rigidbodies to each stone Object with a collider.
It is used to increase physics performance.
What happens when you move a Static Collider:
Don't. When GameObject is made to be Static Collider, it is expected that you never move it. If you do, it will spike cause spike in CPU. This introduces constant freezing in your game each time you try to move it. This spike has been reduced in Unity 5 but it is still there. I think that's what that comment is trying to say. This whole thing happens because it has to recalculate the physical world all over again. From Unity's website, below is a screenshot of what happens when you try to move a Static Collider Object.
A static game object is not the same as a static rigidbody/collider, they're two completely different concept.
Static game objects are used to save performance time/memory regarding rendering, navigation meshes, etc., you can read about them here: https://docs.unity3d.com/Manual/StaticObjects.html
Static colliders are a totally different beast. A static collider is used to tell Unity that this object will never move under any circumstance, so it will save quite some calculations in the physics engine.
Finally, regarding the tip that you mentioned: I think that probably the author of that tip was talking about 3D colliders, and by "static" he meant that you should separate the 3D mesh from the 3D collider, so when the mesh must be rotated, you rotate only that and not the collider.
Think of a sphere, you create a parent game object, then two children: one that has the Mesh component, and the other the Sphere collider. When you want to rotate the sphere in place, you only rotate the mesh child and not the collider, and when you want to translate instead the sphere, you just translate the parent game object in order to change the position of both children.