Question already answered.
Use Layer-based Collision detection
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);
}
}
You want to pick up a coin you are looking at? Use raycasts, not a huge collider. This would solve all your problems.
Answer by tanoshimi · Jun 03, 2016 at 08:01 AM
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 http://docs.unity3d.com/Manual/LayerBasedCollision.html
Answer by JVince · Jun 03, 2016 at 11:10 AM
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
}
Follow this Question
Related Questions
Can I detect a collision between two triggers on other objects 1 Answer
My collision is not detecting 1 Answer
Best way to do Collision detection when you have multiple enemies/allies around you 0 Answers
Checking for collisions 1 Answer
How to detect a collision. Coming back to using Unity after taking a break for a few years. 0 Answers