Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by larumerumba · Aug 12, 2013 at 12:14 PM · oncollisionentercollision-detection

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... :-(

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image larumerumba · Aug 14, 2013 at 07:39 AM 0
Share

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! :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision script 2 Answers

Make Score Go Up At a Collision 2 Answers

Register collision between two specific game objects, using var (not tags or names). 1 Answer

OnCollisionEnter won't fire? 2 Answers

How to get OnCollisionEnter working with Terrain. JavaScript 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges