Is it possible to have two 2D colliders touch and then ignore each other while still listening for collisions with other objects?
Say you have a room, with spikes on the walls and ceiling and floor. In that room is a floating object in which its movement is controlled by the player. In that same room there are other objects floating in space.
What I'm trying to accomplish is once the object you control touches a free floating object they stick together and move as one object. At the same time, if any of the single objects in the group touch a spike, it gets destroyed, leaving the other object it was once attached to intact.
I've tried parenting to no success. I'm new to Unity and I'm not a programmer. I'm using Bolt. I understand the basic logic of how things work so I can probably figure out any code you may type. From what I understand all I need to do is get objects to ignore each other once they collide, but only ignore each other and still look for collisions elsewhere. Once they've collided they need to take input from the player as if they were a single object.
The easiest solution would be to just replace the sprite with a new sprite that shows two objects instead of one, but I was hoping to give the player better interactivity by showing a specific object getting destroyed in real time. Thanks. I hope I explained it clearly enough for you.
Answer by RobAnthem · Feb 22, 2018 at 10:39 PM
Perhaps you are looking for Physics2D.IgnoreCollision? The Physics.IgnoreCollision functions are intended for you to allow two objects to ignore eachother, without it ignoring an entire layer.
Here is the documentation Physics2D.IgnoreCollision Also an example, pulled straight from the docs.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform bulletPrefab;
void Start() {
Transform bullet = Instantiate(bulletPrefab) as Transform;
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
}
This makes it so that the instantiated bullet will ignore the object instantiating it, but obviously you can use this is any way you would need.
If you simply want an object to ignore another, and you aren't a programmer, this will do the trick. Attach the script to the object you want to move that has a Collider2D, and drag and drop the object you want to ignore that has a Collider2D into the ignore slot on the inspector.
using UnityEngine;
using System.Collections;
public class IgnoreCollider: MonoBehaviour {
public Collider2D Ignore;
void Start() {
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), Ignore);
}
}
Thanks for the effort, but I don't think this will work since IgnoreCollision is not persistent. So if the player were to collide their object with an object and combine them and then collide with yet another object to combine with it wouldn't work. The solution needs to continually add to the list of colliders that ignore each other once they've combined together while remaining active to collide with other objects in the scene.
Then what you may be looking for is Physics2D.IgnoreLayerCollision This will allow you to prevent 2 layers from colliding with eachother.
To make it properly work, you would need to setup the layers on the object you want to be collided with only once, and create a secondary layer that is for anti-colliding, then do something like this.
public int acquiredLayer;
public int unAcquireLayer;
void Start()
{
Physics2D.IgnoreLayerCollision(acquiredLayer, unAcquireLayer, true);
}
void OnCollisionEnter(Collision col)
{
if (col.collider.gameObject.layer == unAcquireLayer)
{
if (col.collider.gameObject == objectImLookingFor)
{
col.collider.gameObject.layer = acquiredLayer;
//do other logic code here.
}
}
}
The collision layers is what I was missing out on. Thanks!
Your answer