How to animate rigidbody objects in Unity - c#

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.

Related

Sibling game objects not colliding in Unity2D

I have a game in which waves of enemies are spawned by storing an arrangement of enemies as children of an empty game object, which I can then save as a prefab and instantiate with an enemy spawner script, but there is no collision between the enemies, and the only reason I can think of is because of some physics optimization due to them being siblings. These are my collider and rigidbody settings for the enemies: Any idea why this happens? Thanks!
This is likely because your Rigidbody constraints are set to freeze movement on both the X and Y axes:
This means that any movement you would normally expect from simulated physical interactions (for example, changing velocity from colliding with another enemy) will be ignored for this Rigidbody. See the Unity docs for more details on what they do.
The simple fix here would be to uncheck those checkboxes, so that you rely on the physics system to handle these interactions. If you want to fully control them through code though, you could technically write code in OnCollisionEnter() to handle the collision between two enemies - but that would be a much more involved solution.

Unity3D Rigidbodies acts like objects is elastic and falling inside eachother

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.

Physics ignore to avoid collision

I am aware of using Physics.Collision to avoid collision between two certain objects. But I want their box colliders to be active so that it detects that they are in contact. Is there a way to achieve this?
Also after I have disabled collision using Physics.Collision(), can I reactivate the collision between the objects?
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>());
In the Unity menu bar, go to Edit > Project Settings, then select the Physics category to open the Physics window. You will find a Matrix and here you can uncheck the collision between 2 layers. If you put the Player in a layer and the object you don't want to collide into in another layer and in the matrix you uncheck the box, everything will maintain his properties and the player won't collide with it.
See Physics.IgnoreCollision
ignore: Whether or not the collisions between the two colliders should be ignored or not.
Per default it is true.
To re-enable collisions you previously disabled simply pass in false.
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>(), false);
If you don't want the objects to collide but register if they touch each other then make on of them a Trigger.
If the object is a trigger then it doesn't react to the collision but it fires an OnTriggerEnter.
See Colliders -> Triggers
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine
simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts
.

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.

Unity 2D How to get Object that is being overlapped

I have blocks in a scene and I move a block with the mouse. Each block has a BoxCollider2D attached. I am trying to figure out which block it is that I bumped into. I can't use Rigidbody to detect collisions as I don't want any physics applied. I know how to detect if the object I am moving is overlapping a point but I don't know how to get the object that I am overlapping?
You don't need physics/rigidbody to use colliders; just set the colliders as triggers and use the OnTrigger method.

Categories

Resources