Unity3D Rigidbodies acts like objects is elastic and falling inside eachother - c#

I was making a game about stacking objects on top of eachother but when I spawn a new object above it falls inside in other object and behaves like elastic material.
I didn't specify any Physics Material to the Rigidbody, It's just a vanilla Rigidbody component.
Here is a gif about my problem

You might have forgotten to add a Box collider to the cube prefab.

Make sure that the collision detection method on the rigidbody is set to either continuous or continuous dynamic. Make sure there's a collider as well.

Related

Making an object move through another and keep going?

I am currently making a sidescrolling game in Unity 2D where an enemy rises up, and then dives down to the player, but then keeps going until it is off screen, like how this image shows. 1 I can get the enemy to collide with the player but I can't make it keeping in that direction until off screen. How would I be able to do this?
You have to make your collider to trigger (it's a checkbox on your collider)
That means your object will not be affected by physics when it touch another collider, but it can calls functions.
You have to replace OnCollisionEnter/OnCollisionEnter2D, etc by OnTriggerEnter/OnTriggerEnter2D, etc.
Be careful, the parameter of the function may change too (Collision/Collider).

How to make my button change on collision

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script
script : https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d
I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.
Thanks in advance
It is important to distinguish the difference between Colliders and Triggers.
Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.
In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.
As such it is also important to know which functions you should use in each case.
OnCollision... For Colliders, (Or Colliders not marked as triggers)
OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)
To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.
As such there are functions for each 2d, 3d, collision, and trigger.
Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).
As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

Colliders not working in Unity3D 2D Project

I have 2 colliders, its a game where I have a ball, and a Labyritnh. The ball should pass through the labyrinth and collides with the labyrinth's walls.
But I have a problem, the physic aren't working that well, the collider don't work.
I have a rigidbody and a collider in both of these objects.
Also, I have a problem with choosing box or edge colliders.
Because it is a labyrinth, I need to design the colliders with some complex shapes.
I have used edges colliders and box colliders but the ball is passing through all these colliders like nothing, It seems like its not working.
Can someone help me?
you need a rigid body object attached to at least one of them for the colloders to work.
make each wall individually and use rectangel colliders. add a rigidbody component, and set that rigid body to kinematic. should work then.

Collision between Box Collider 2D and Polygon Collider 2D not working

Currently I am working on a networked 2d platform game.
My player is an empty object which serves as a parent to my actual graphics etc. The empty object has a rigidbody (3d) attached to it as it needs to have one in order to use the configurable joint component ( note: I move my player around using this 3d rigid body).The player body however, a child to the graphics, has a box collider 2d attached to it as it after all it is a 2d game and has to collide with other 2d objects.
My platforms have polygon collider 2ds attached to them (which is connected to a platform effector if that is worth mentioning).
Now, even though both my player body (which is a child to graphics which is a child to my player object) and my platforms have 2d colliders on them they do not collide and can simply go inside each other (neither of them is marked as isTrigger).
In order to solve this I thought I would add a rigidbody 2D to the player body and see if that would do anything. Now upon adding the rigidbody 2D collisions did work but as soon as I made the rigidbody 2D have all position and rotation constraints ticked or as soon as I made it kinematic or static it would cease to collide with my platforms. The problem is I need to have the rigidbody 2D be static or not be able to move as I am currently moving my player object using the rigidbody (3d) attached to it and do not want any additional movement of the player body upon colliding with objects.
I know this is quite a lot of information, so if you have any questions or would like further information just comment and I will be quick to respond. Thank you :-)!
Edit:
2d ray casts are also unable to hit the player body box collider 2D
Edit 2:
So to recap:
If either the player body or the platform has a rigidbody 2d that is not static (dynamic) and can move collisions work. However currently I only have a rigidbody 2D on my player body which has to be static though (as explained earlier) as well as a box collider 2d. My platform(s) on the other hand currently only has a polygon collider 2d as I do not see why it would need a rigidbody 2D.
A static collider won't detect the collision with another collider if it doesn't have a rigidbody, if it is static or if it has a kinematic rigidbody. At least one of your two objects needs to not be static and to have a rigidbody which is not kinematic to be correctly detected. So you do need to add a Rigidbody2D to your platform.
When you have a doubt about why a collider wouldn't collide with another one, always refer to this page of Unity's documentation which sums up which kind of collider will collide with another one.
Rigidbody2D can't collide with Rigidbody3D, here's a workaround though
http://answers.unity3d.com/questions/580769/can-rigidbody-2d-collide-with-3d-colliders.html

How to animate rigidbody objects in Unity

I'm creating a game in which I need to move an object straight up, and when it hits another object they are connected with joint. That's why I need to have rigidbodies attached to both of them. The problem is that when I use animation to do this, collision is not detected, and unity tends to crash. Is there any proper way of "animating" rigidbody objects? Or maybe I should choose a different approach?
Set Rigidbody.isKinematic to true on the rigid bodies just before animating them. This will allow you to move (animate) the rigid bodies by changing the transform.position and other properties while allowing collisions with other non-kinematic bodies(rigid bodies with isKinematic set to false) and joint constraints to work properly. When the animation has completed and you want the bodies to be affected by physics again, set isKinematic to false.
Refer to the documentation for isKinematic for more information and a ragdoll example.

Categories

Resources