- Home /
Ignore collisions with self but still trigger with self
I have a script which instantiates a bunch of objects called SPHERE. They are on a ballLayer which prevents them from colliding with each other, but they can still collide with the walls which are on the default layer. What I want is for them to still be able to trigger with each other as well. I'm not sure how I could go about this because if i set their collider to istrigger, then they no longer work with the unity physics engine. Any help would be appreciated.
Well, remove the layer then. What's the point in preventing them to collide with each other if you want to collide with each other? As for isTrigger, instead of doing the physics collision, it sents messages so you can deal with it manually:
https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html
Answer by LadyOfTheMoon · Nov 16, 2020 at 06:17 PM
You could try adding a child object the sphere. Give the child object an identical collider but enable isTrigger and set it to the default layer.
Then you can check for triggers on the child object while collisions are handled by the parent.
The alternative solution would be to check if the ball is inside anything manually using Physics.OverlapSphere every FixedUpdate. Something like this:
void FixedUpdate()
{
Collider[] colliders = Physics.OverlapSphere(rigidbody.position, radius, ballLayer);
if (colliders.Length > 1)
{
// More than one ball is inside the area, meaning this ball is overlapping another
}
}
if isTrigger is enabled on the child object, then wouldn't it just phase through the walls? or would I set it's position to the parent objects position every fixed update? Does OverlapSphere work if the balls are on a layer in which they don't collide with eachother? or does the OverlapSphere just check for any collider on any layer?
Child objects move with their parent so you don't need to update it's position manually, it won't pass through the wall because the parent will collide with it. I made a quick setup like this and it seems to work:
OverlapSphere should work regardless of collision settings, it just returns a list of colliders that overlap in the given area, you can search for colliders on a specific layer or across all layers.
Okay thanks a lot, I have a lot of objects and they are all gonna have to make this check so which do you think would be less intensive?
Answer by $$anonymous$$ · Nov 16, 2020 at 09:50 AM
Use Physics.OverlapCircle and then use a Rigidbody.AddForce once collided.
Here Check this out for Physics.OverlapCircle And this for Rigidbody.AddForce
Could you elaborate a bit on that? would the balls need to be able to collide for overlapCircle to work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletCollisionScript : $$anonymous$$onoBehaviour {
public float radius;
public Layer$$anonymous$$ask whatIsTheBulletLayer;
public Collider2D[] collidedCols;
private Rigidbody2D rb;
void Start(){
rb = GetComponent<Rigidbody2D> ();
}
void Update(){
// Gets all the bullets in the circle
collidedCols = Physics2D.OverlapCircleAll (transform.position, radius, whatIsTheBulletLayer);
if (collidedCols.Length > 1) {
// Once the CollidedCol.Length > 1
// Which means the bulletCollidedWithSomething
// This Code will run
}
}
void OnDrawGizmosSelected(){
Gizmos.color = Color.green;
// Helps you see the radius of the circle in the inspector
Gizmos.DrawWireSphere (transform.position, radius);
}
}