Advertisement

Two instantiated gameobjects of same prefab are not triggering collision with each other

Started by October 24, 2018 07:39 AM
4 comments, last by Abhimanyu Bundela 6 years, 1 month ago

I'm working on some collision game, I have 2 prefabs 1 is player and other is Guard But when I instantiate Guard prefab 2 times and they collide with each other nothing happens. But when they collide with the player it works completely fine. I'm using Tags for the Detection. Here's my code:

 


using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GuardCollision : MonoBehaviour {
 
     public Vector3 spawnValue;
         
     void OnCollisionEnter (Collision collider){
         if (collider.gameObject.CompareTag ("Guard")) {
             Debug.Log ("It's a guard");
         } else if (collider.gameObject.CompareTag ("Player")) {
             Debug.Log ("It's a Player");
         }
     }
 
 }

 

You might add an 'else' block to your logging code there to indicate if a collision occurred, but the tag wasn't recognized. That might tell you something.

Also, if I recall correctly the Unity physics system has a collision matrix where you can specify what physics 'layers' interact with one another. If you determine that the OnCollisionEnter() function isn't being called at all, you might double-check that matrix to make sure interaction between your 'guard' objects is enabled.

Advertisement

Do your prefabs have Rigidbody components? Generally Unity treats an object with a collider but no Rigidbody as a piece of level geometry which cannot move but can be hit by other objects. Having overlapping colliders is pretty common when creating level geometry so static colliders cannot collide with each other. Check the "Collision Action Matrix" section of the Unity manual (link below) to see if your setup is something that will generate a response.

From the Unity manual https://docs.unity3d.com/Manual/CollidersOverview.html

 

Quote

 

Static Collider

This is a GameObject
that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.

The physics engine assumes that static colliders never move or change and can make useful optimizations based on this assumption. Consequently, static colliders should not be disabled/enabled, moved or scaled during gameplay.

 

 

--------------------------Insert witty comment here!--------------------------
On 10/24/2018 at 4:39 PM, Abhimanyu Bundela said:

I'm working on some collision game, I have 2 prefabs 1 is player and other is Guard But when I instantiate Guard prefab 2 times and they collide with each other nothing happens. But when they collide with the player it works completely fine. I'm using Tags for the Detection. Here's my code:

 



using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GuardCollision : MonoBehaviour {
 
     public Vector3 spawnValue;
         
     void OnCollisionEnter (Collision collider){
         if (collider.gameObject.CompareTag ("Guard")) {
             Debug.Log ("It's a guard");
         } else if (collider.gameObject.CompareTag ("Player")) {
             Debug.Log ("It's a Player");
         }
     }
 
 }

 

maybe you can solve it with OnCollisionStay();

just try it

9 minutes ago, Sylon87 said:

maybe you can solve it with OnCollisionStay();

just try it

I finally figured it out, actually it's just because of a Obstacle avoidance property of a guard(AIThirdPersonController).Because of which it only collide with its target and avoid everything else with the mentioned safe radius.

Reduce the Obstacle avoidance radius to minimum then it will also collide with other game objects.

You can change that in Inspector menu of Guard.

Thanks for the reply

 

 

 

This topic is closed to new replies.

Advertisement