1
votes

I'm trying to make a BTD game. Since different balloons (enemies) have different speeds, they collide with each other when traveling on the path. I'm currently using this code:

void OnCollisionEnter (Collision coll)
{
    if (coll.gameObject.tag == "Enemy")
    {
        Physics.IgnoreCollision(coll.collider, gameObject.GetComponent<SphereCollider>());

    }
}

However, it doesn't appear to be working at all. The enemies still collide with each other. On the otherhand, the collision with the enemies and bullets from towers is working.

void OnTriggerEnter (Collider col)
{
    if (col.tag == "Bullet")
    {
        CurrentHP -= col.GetComponent<TackShooterBullet>().Damage;
    }  

I've tried layer-collision (enemies to same layer & unchecking of the same layer collision in the layer collision matrix, but that doesn't work either. The enemy contains a sphere mesh filter, sphere collider, mesh renderer, rigidbody, material, and 2 scripts. Is there a better way to avoid collisions between the enemies. I'm asking this question since I've seen duplicates, but their solutions aren't working at all. I can provide more of my code if needed.

Edit for Clarity: Again, what I'm trying to accomplish is have the enemies be able to go through each other.

Edit (Fixed Problem): I found out to avoid Enemy Collisions, I could also remove rigidbody. However, removing the ridigbody would mess up the bullet --> enemy trigger in the enemy class. Therefore, I just wrote the collision between bullet & enemy in the bullet class instead.

using UnityEngine;

public class TackShooterBullet : MonoBehaviour {

private GameObject target;
public float Damage;

// Use this for initialization
void Start () {
    target = transform.parent.GetComponent<TackShooterRange>().Target; // Target = Enemy[0] (First Enemy To Enter Range - Enemy is Removed from JList when exiting Range)
}

// Update is called once per frame
void Update()
{

    Damage = gameObject.transform.parent.transform.parent.GetComponent<TackShooterLimitingRange1>().level * 20; // Upgrade Level * 20 = Damage Done

    if (target == null) // If Enemy Exits Range
    {
        Destroy(gameObject); // Destroy Bullet
    }
    if (target != null) // Enemy Exists In Range
    {
        transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 20 * Time.deltaTime); // Bullet Follows Enemy
        Destroy(gameObject); // Destroy Bullet Upon Contact With Enemy
        target.GetComponent<HealthOfEnemy>().CurrentHP -= Damage; // Enemy Loses Health
    }
}

This allowed me to remove the OnTriggerEnter & OnCollisionEnter methods as well as the RigidBody from the Enemy Class as stated before, so these properties no longer affect the collisions between Enemies.

1
Put Debug.Log and before Physics.IgnoreCollision(coll.collider, gameObject.GetComponent<SphereCollider>()); and see if Collision is actually detected! - Programmer
You're right. Collision isn't detected (No Message Appears). Under hover, Coll.Collider says the collider you hit. What is the name of the gameObject's own collider? - Infinity Max
"Under hover, Coll.Collider says the collider you hit" I don't uderstand what you are trying to say. Since there is no log, why put Debug.Log("1") before the if statement then put another Debug.Log("2") inside the if statement. We have to know where your code is failing - Programmer
Ok. I was checking out something and I found out if the Rigid Body is Removed from the Enemies during Collision, they pass through each other. Would changing the RigidBody be better? - Infinity Max
I've updated the solution I found which just removed RigidBody and didn't need use of the collision method. - Infinity Max

1 Answers

0
votes

Unity has a built in function for easier collision detection called layer-based collision detection:

https://docs.unity3d.com/Manual/LayerBasedCollision.html

The documentation is really good. Just comment if you need further clarification.