Collision Detection not working
I have a cube being fired from mouse location and I want it to do something when it hits a wall. Simple, right? Well, I have been stuck on collision detection for days now. I tried adding a script to the spawned cube, adding a tag to the collision GameObject, and write this code :
using UnityEngine;
using System.Collections;
public class CakeCollisions : MonoBehaviour {
public GameObject collisionWall;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col)
{
switch (col.gameObject.tag)
{
case "CollisionTag":
Debug.Log("Collided");
break;
}
}
}
Does not work. Tried the other way around (Script on collision which is placed on level from start of game, and tagging the cube instead. Also doesn't work. Have been watching tutorials, following things by line, what am I doing wrong?
Yes, its a 2D game, however the cube is 3D and firing in X axis. Is it a RigidBody or RigidBody2D?
Answer by seckincengiz · Jun 22, 2016 at 06:00 PM
I understand what you are trying to do but you can achieve the same result just using OnTriggerEnter method. I wrote a simple example below. If this is not the case (If you must use collisions), make sure that one of your object has rigidbody component. Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. More Info : https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
Also try this instead of using switch-case:
if(col.gameObject.tag == "CollisionTag")
{
Debug.Log("Collided")
}
OnTriggerEnter Method
//Attach this script to the projectile
//Make sure your wall has a tag in your case "CollisionTag"
//Make sure that the wall has collider and it's Is Triggered proporty is checked.
//If you also want collision effect; add another collider to your wall. And this time
//Is Triggered proporty Unchecked
//If you instantiate the projectile you should apply this script to root object and apply.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnTriggerEnter(Collider other) {
//Note: we use colliders here, not collisions
if(other.gameObject.tag == "CollisionTag"){
Debug.Log("Collided");
}
}
Answer by Graphics_Dev · Jun 21, 2016 at 07:38 PM
You probably need to enable "Is Trigger" on the collider component.
Answer by BenCrew · Jun 21, 2016 at 03:04 PM
How fast are the cubes going? They may be going to fast and passing through the wall.
have you tried to reduce the speed to see if that helps? also have you tried break pointing and stepping through the code?
that's probably not the problem then. have you tried doing a general collision with "other" just to check its not just yours collision object you have a problem with?
Answer by unity_ngP8_Fl6_Peiuw · Nov 27, 2018 at 10:20 PM
Check the layer that the offending object is on. I had the same sort of 'it isn't colliding' problem and none of the discussion mentioned layers. The object I was having problems with was assigned to the 'default' layer and everything else was on 'environment' or 'terrain' or 'player' and it wasn't until I asked the question about what layers are used for that I found out that in the Edit->project->physics menu there is a table of layers and you can select those that do or do not collide with each other. See https://docs.unity3d.com/Manual/LayerBasedCollision.html