Avoid physical collision between tagged gameObjects

This script avoid collision between 2  rigidbody/collider with a specified Tag name.

Let’s create a new script :  and copy this into it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class avoidByTag : MonoBehaviour {
 
	public string tagName;
 
void OnCollisionEnter(Collision collision){
 if(collision.gameObject.tag == tagName){
  Physics.IgnoreCollision(collision.collider, GetComponent() );
 }
}
 
}

 

just drag & drop it into your gameObject that must avoid some collisions, and in the editor just fill the “tagName” with the tag that your gameObject should pass through.

Leave a Reply