11

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

user145708
  • 121
  • 1
  • 1
  • 5
  • How are you moving the object? It looks like that's in your Addforce script. Please post that code. – Programmer May 12 '17 at 13:41
  • Are you sure your object is tagged "Collider"? – Daahrien May 12 '17 at 14:15
  • 100% sure, however it ain't the object that is taged "collider" but rather the object has a cube child and that child has the box collider and the "Collider" tag, can that be the problem? also, i added the "add force" script – user145708 May 12 '17 at 18:29

5 Answers5

26

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

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Kleber
  • 942
  • 1
  • 15
  • 25
  • I've already checked the first 2, the red cell has a collider on it, however the wall doesn't, however the parent of the wall (which is the vein) has a rigidbody, also the tag is on the childed wall not on the vein, can that be the problem? if so how can i do the object disappear when it crashes that specific pink collider? – user145708 May 12 '17 at 18:41
  • If the Red Cell is inside the collider of the Wall's parent, the Collision**Enter** event will never occur. If you want to detect the collision between the Red Cell and the Wall, the Red Cell and each Wall needs to have a Collider. The Rigidbody can be in the Red Cell or in each Wall or both. The vein possibly doesn't need a collider if no GameObject will actually collide with it. Think like this: every GameObject that collides needs a collider and potentially a Rigidbody. The collider tells the physics what to expect for that object individually. – Kleber May 13 '17 at 17:56
2

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.

PedPak
  • 157
  • 1
  • 3
1

I have a few followup questions which might lead to a solution.

First, does the object holding your 'collision' script have a rigidbody and a collider on it? Second, does the wall have both a rigidbody and collider?

Usually if those conditions are met, then collisions will work. A couple other things that could be the problem:

  1. Check if you have istrigger checked for either object and make sure it is unchecked.
  2. Check and make sure the rigidbodies on both are non-kinematic.
Nick Crow
  • 11
  • 2
  • The object that is holding the collision(in this case the red cell) script has a rigidbody, and the wall just have a collider, however the parent of the wall (which is the vein) has a rigidbody – user145708 May 12 '17 at 18:38
  • You cannot use the parent of the object. It needs to be the same one that has a collider will have the rigidbody. As for the red cell, it needs a collider. there need to be 2 colliders and at least one rigidbody. – Nick Crow May 13 '17 at 06:49
1

I've finally fixed it, I dont really know if this was the problem, but i just removed the rigidbody from the parent of the wall and it started working!, I dont know what the rigidbody did, but just with that the problem was fixed, thank you all for your help! :D

user145708
  • 121
  • 1
  • 1
  • 5
0

Ensure the following things are considered in your code,

  1. All gameobject should contain collider attached and the player gameobject should contain the rigidbody component in it.
  2. The collider size should change to the width and height of the component instead of default (1,1) values.

enter image description here

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81