- Home /
No collision detected between two gameObjects
There are several topics about non-working collision detection, but no solution worked in my case.
I would like to trace the collision between two very simple objects and only want to know at what point they collide, but it should be possible that they overlap and are not knocked back.
There is one GameObject ("Volume") that is moving and when it fades into another GameObject ("Shape") an attached sript should notify! Simple, but somehow not working. These GameObjects are created dynammically with an button event while the program is running.
The Volume is created like this:
//---------------------------------------------------------------------
private static GameObject createVolume( float moduleWidth, float moduleHeight, string textureName )
{
GameObject cube = Resources.Load( "Prefabs/Volume", typeof( GameObject ) ) as GameObject;
//GameObject cube = GameObject.CreatePrimitive( PrimitiveType.Cube );
// Assigns a material named "Assets/Resources/ReflectionMaterial" to the object.
Material newMat2 = Resources.Load( textureName, typeof( Material ) ) as Material;
cube.renderer.material = newMat2;
//cube.renderer.castShadows = false;
//cube.renderer.receiveShadows = false;
cube.rigidbody.useGravity = false;
cube.rigidbody.freezeRotation = true;
//cube.rigidbody.freezePosition = false;
cube.collider.isTrigger = false;
// set size
cube.transform.localScale = new Vector3( moduleWidth, 1f, moduleHeight );
return cube;
}
so, there is also the script attached to this Volume that should trace the collision:
using System;
using System.Collections.Generic;
using UnityEngine;
public class GEO_VolumeScript : MonoBehaviour
{
//---------------------------------------------------------------------
void OnCollisionEnter( Collision col )
{
Debug.Log( "COLLISION ENTER " + col.gameObject.name );
if( col.gameObject.transform.tag.Equals( U3D_Definitions.SHAPE_TAG ) )
{
U3D_ResultManager.PrintResultHit( this.gameObject, col );
U3D_ModelManager.DrawHit( col.gameObject );
}
}
//---------------------------------------------------------------------
void OnCollisionStay( Collision col )
{
Debug.Log( "COLLISION STAY " + col.gameObject.name );
// count the time of collision
}
//---------------------------------------------------------------------
void OnCollisionExit( Collision col )
{
// trigger end of collision
}
//---------------------------------------------------------------------
//void Update()
//{
// // this is working
// Debug.Log( "Update" );
//}
}
and the Shape is created like this:
public static GameObject CreateShape( Vector3 position, float width, float height, float depth, Quaternion orientation )
{
Debug.Log( "GEO_ShapeFactory:: CreateShape()" );
position.y += height;
// create shape and clone it
GameObject shapeObject = GameObject.CreatePrimitive( PrimitiveType.Cube );
GameObject shapeObjectClone = ( GameObject ) Instantiate( shapeObject, position, orientation );
shapeObjectClone.transform.localScale = new Vector3( width, height, depth );
shapeObjectClone.collider.tag = U3D_Definitions.SHAPE_TAG;
// add material to shape
GEO_ShapeFactory.setGeometryObjectHighlighted( true, shapeObjectClone );
Destroy( shapeObject );
return shapeObjectClone;
}
So, from the script I get no response at all. All objects have box colliders added. The Volume has a rigidbody, where gravity is deselected and the position and the rotation are frozen. If I add a rigidbody to the Shape the object is knocked back in the scene, so the collision is taking place, but the script doesn't tell. I tried the Update() method there to see if there is actually anything happening - positive... so the script is there, recognized, working but without the OnCollisionEnter method... :-(
Answer by Xtro · Aug 12, 2013 at 01:51 PM
If you want them to overlap, at least one of them must be marked as trigger. In this case, you can't use OnCollision events. You should use OnTrigger events.
So, I got the solution. The OnTrigger is absolutely correct, thank you Xtro, but didn't work for me either in the first place. What I found out was that the error was with the collider. The vertices of the volume have been changed by me, but I was not aware that the mesh collider stayed the same. After updating the mesh collider with...
// rescale the mesh collider of the reflection volume
reflCube.transform.GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = reflCube$$anonymous$$esh;
... everything was finally working as it was supposed to! :-)