Selective Collision Detection

Hi everyone.

I’m trying to implement a projectile magic system, and at the moment what I have is a system that destroys the spell when it collides with anything, using “OnTriggerEnter”.

The problem is that I also have an Elder Scrolls style crosshair set up, which is a long capsule collider that extends out from the centre of the screen (the idea being that if it collides with an item, like a gold coin, I can pick up the coin). The spell will destroy itself immediately after being instantiated because it detects the crosshair collider.

I need some sort of way of making the OnTriggerEnter check for every object other than the crosshair. I’m using C# and I’m not thoroughly experienced with it, but there must be some sort of “Ignore Tag” system or something.

I tried a “StopCoroutine” line when it detects the crosshair, but this cancels out its destruction upon collision with other objects if it’s still touching the crosshair. So if I stand up against a wall and shoot the spell, the spell would go through the wall because its destruction would be cancelled out due to still touching the crosshair.

Thank you for your help!

using UnityEngine;
using System.Collections;

public class ProjectileMagic : MonoBehaviour {

	public int damage;
	public float speed;
	private EnemyScript targetScript;

	void Start () {
		StartCoroutine ("SpellLife");
	}
	
	void Update () {
		transform.position += transform.forward * Time.deltaTime * speed;
	}

	void OnTriggerEnter (Collider other)
	{
		StartCoroutine ("DestroySelf");

		if (other.gameObject.CompareTag ("Enemy")) {
			targetScript = other.gameObject.GetComponent<EnemyScript> ();
			targetScript.healthPoints = targetScript.healthPoints - damage;
			StartCoroutine ("DestroySelf");
		}
	}

	IEnumerator DestroySelf () {
		yield return new WaitForSeconds (0);
		Destroy (gameObject);
	}

	IEnumerator SpellLife () {
		yield return new WaitForSeconds (20);
		Destroy (gameObject);
	}
}

As @allenallenallen commented, you’d be better off raycasting than using a collider for your crosshair. But, either way, you implement selective collision detection using layers, as explained at Unity - Manual: Layer-based collision detection

If you just want check collision on trigger, I think this is what you want. And if you wanna pick up item, I agree with tanoshimi Reply and allenallenallen commented.

int igObj=0;
void OnTriggerEnter(Collider col){
       if(col.compareTag("ignoreItem")){
             igObj++;
       }
       if (igObj != 0){
             //Some Amazing Code
       }
    }
    
    void OnTriggerExit(Collider col){
       if(col.compareTag("ignoreItem")){
             igObj--;
       }
       //Some amazing Code
    }