Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collision detection not working unity

First, I know that this question has been asked a lot, but I cant find a solution, so mi problem is, Im making an educational game, and I have a vein and the blood flow (with many box colliders) and a single blood cell (also with a box collider) however i want the cell to destroy when it reaches the wall collider, but it doesn't it just stays there, here is the project!

http://tinypic.com/r/10706es/9

(cant upload images because of my reputation, sorry)

The collider where I want to destroy my cell is the pink collider, however when it touches it it just does nothing, here's my script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collision : MonoBehaviour {
    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void OnCollisionEnter(Collision col)
    {
        print("hihi");
        if (col.gameObject.tag == "Collider")
        {
            Destroy(gameObject);
        }
    }
}

Also, here is the AddForce script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddForce : MonoBehaviour {

    public float thrust;
    public Rigidbody rb;
    private Vector3 up;
    private bool move;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        up = new Vector3(0, 1, 0);
        move = false;
    }

    void FixedUpdate()
    {
        if (Input.GetKey("space"))
        {
            if (rb.velocity.magnitude < 5)
                rb.AddForce(up * thrust);
            move = true;
        }

        else
        {
            if (move == true)
                rb.velocity = new Vector3(0, -0.5F, 0);
        }


    }
}

thanks for your help guys! :D

like image 758
user145708 Avatar asked May 12 '17 13:05

user145708


People also ask

Why is collision detection not working Unity?

A collision can not be detected if the Is Trigger attribute is set to the collider. Open the properties pane of the game object and look inside the Collider component to ensure that option is not selected.

Do you need a Rigidbody to detect collision Unity?

In Unity3D, you need rigidbodies on GameObjects that use colliders, as they use physics. You don't need rigidbodies on static GameObjects because they don't use physics, though you still need to have at least one in the calculation.

Does OnTriggerEnter need Rigidbody?

In order to generate an OnTriggerEnter(Collider other) message, at least one of the colliders involved has to have the isTrigger flag set, and at least one of the objects involved has to have a Rigidbody attached (either at the same level or in one of its parents).


2 Answers

It can be several things, whether you are using OnTriggerEnter or OnCollisionEnter:

  • Missing RigidBody (the most common). At least one of the GameObjects involved needs to have a RigidBody. (check if at least one of them have a RigidBody attached and, if you are using OnCollisionEnter, does not have the "Is Kinematic" checked). See the below collision matrix for more information.

  • Missing tag. The GameObject from collision does not have a "Collider" tag (try to remove the if statement to test it) (to compare tags, use collider.gameObject.CompareTag("Collider"), it has a better performance)

  • Undetectable collision. The physics Layer Collision Matrix is set to not detect collision between the layers the objects are (enter Edit > Project > Phisics and check if the encounter of the layer of both GameObjects are checked inside Layer Collision Matrix)

  • Wrong Collider configuration. one or both of the GameObjects have a small/wrong placed or absent Collider (check if they both have a Collider component and if their size are correct)

If it's working, you should be able to press play and drag one GameObject into the other one and your Debug.Log will appear.

As an advice, use tag names that better describe the group of GameObjects that will be part of it, like "RedCells" or "WhiteCells". It'll be easier to configure the Layer Collision Matrix and improve the performance of your game.

Another advice: for colliders that just destroys another GameObject (don't react, like bump or actually collide) I use triggers. That way, the collision between them will not alter anything in the remaining GameObject (like direction/velocity/etc). To do that, check the Is Trigger in the Collider and use OnTriggerEnter instead of OnCollisionEnter.

Collision matrix
Source

like image 164
Kleber Avatar answered Oct 08 '22 02:10

Kleber


some times you added Nav Mesh Agent component to your game object ( to auto route operation in strategic game and ...). in this case, this game object does not attend to collider. So, if you really need this Nav Mesh Agent, you should add Nav Mesh Obstacle to other fixed game object and also add Nav Mesh Agent to other movable game object.

like image 38
PedPak Avatar answered Oct 08 '22 02:10

PedPak