Physics ignore to avoid collision - c#

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
.

Related

Disable only one collider when two colliders are touching

I'm making a game in Unity 2D and was wondering if there was any way to do what I need.
Please check the image of Two items with hitting colliders.
When the player is activating both colliders and then the pick up button is pressed, the player equips both items. I want to disable one of the item's colliders when another item is nearby and then re-enable it when its by itself again. How should I go about doing this? I was thinking using a list but since both weapons use the same scripts (they're prefabs) I think that would create two lists that would serve no purpose and probably crash.
My initial check was to see if another item was nearby, and if so disable its collider, but it did that for both the items rendering both useless.
You can put a Boolean flag in the player code
something like :
if(hasWeapon == false)
{
//put pick up code here
}
so that if a weapon is selected, it will not be entered into the pick-up code until you drop the weapon.

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.

Unity3D: How do I ignore a collision without disabling future collisions?

I've got a 2D flocking object that uses a nested trigger collider to establish separation and a normal collider to handle collisions with other objects. (The collider is set to ignore everything in the same layer.)
I don't really like this setup, and would rather fold the avoidance code into the parent object's OnCollisionEnter2D method. Physics2D.IgnoreCollision does what it says on the box, but then all subsequent collisions are ignored.
Is there a way to make two colliders transparent to one another without having them completely ignore one another? Alternatively, is there a way to activate normal collision behaviour from OnTriggerEnter2D?
Or am I being an idiot (as usual) and missing an obvious solution?
You could set that specific collider as trigger.
In example - have a gameobject on a layer that collides with all layers except the one that you want to be 'transparent' and then have another gameobject that only collides with objects on the same layer but set it's collider to trigger.
Would that work for you?

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