Collision Layer Ignoring Script Logic -- If && statements not working correctly


I have a scripting problem that deals with ignoring collision layers. Briefly, I have two static gameobjects with mesh colliders and portions of their meshes overlap each other. This is necessary for my specific application and is fine otherwise. The problem is that i have a non-static rigidbody gameobject (with a box collider) which needs to ‘bump into’ these static colliders and when it does, I need code to run. So, this all works fine when collisions occur on the area where the mesh colliders are not overlapping each other—but causes extreme lag and poor performance when it collides in the overlapping areas.

My solution was to use a little If and && logic to activate/deactivate the collision layers such that only 1 collision layer (i.e., mesh collider) is active at any given time. I thought that this would work well, but the problem is that it keeps one layer activated and the other layer deactivated constantly (for example, layer 1 is active and layer 2 is ignored regardless of the logic). However, if I remove the gameobject that is associated with layer 1 from the scene, then layer 2 is no longer ignored (and vice versa).

Since it is partially working when I remove gameobjects from the scene, my guess is that the problem is with my C# code and the logic that I’ve used. I’m a beginner with both programming and Unity so any help offered will be informative and welcome.

Attached is the relevant portions of the code:

using UnityEngine;

public class CollisionForce : MonoBehaviour {

	void OnCollisionStay(Collision collision)
		//Layer 8 = Patch (one of the static mesh colliders)
		//Layer 9 = Hemi (one of the static mesh colliders)
		//Layer 10 = Peg (the rigidbody gameobject)

		//if peg hits hemi and patch, ignore all hemi collider
		if (collision.gameObject.layer == 9) // && collision.gameObject.layer == 8)
			Physics.IgnoreLayerCollision (10, 9, false);
			Physics.IgnoreLayerCollision (10, 8);

		//if peg hits patch only, ignore all hemi colliders
		if (collision.gameObject.layer == 8) //&&collision.gameObject.layer != 9 &&) 
			Physics.IgnoreLayerCollision (10, 8, false);
			Physics.IgnoreLayerCollision (10, 9);
			Debug.Log ("test2");

		//if peg hits hemi only, ignore all patch colliders
		//if (collision.gameObject.layer == 9 && collision.gameObject.layer != 8) 
		//	Physics.IgnoreLayerCollision (10, 8);
		//	Debug.Log ("test3"); 


I’ve tried both the commented out logic and the current logic and both behave the same way.

You have the wrong idea about boolean logic and you should look up the basics behind them. When you have an if statement like

if (collision.gameObject.layer == 9  && collision.gameObject.layer == 8)

Each condition is seperate from each other. The && operator only evalutates to true when both operands are true. Just by looking at this line you can immediately see that this can never ever happen. If the layer is 9 (so the first operand is true) then the layer can not be 8 at the same time. What you want is to use the “or” operator ||. You want that statement to be true when the layer is either 9 OR 8.

So it should be

if (collision.gameObject.layer == 9  || collision.gameObject.layer == 8)

Likewise in your second case the second condition is pointless. If the layer is 8 of course it will not be 9


I just read your description carefully and just realised that what you want is something different. Though it’s not really clear what actual behaviour you want . Your current code doesn’t make too much sense. You use OnCollisionStay which is called every frame while you have a collision. Though when you collider with an object on layer 8 you disable the collisions with layer 8 so OnCollisionStay will no longer be called when the next physics update happens. So you get more like a trigger behaviour since once you touched that collider you disable the collision so you can no longer collider with it. However if the player is in an area where both colliders overlap, you would essentially switch between the two all the time. So what’s your expected behaviour in those overlapping areas?