Quantcast
Channel: Questions in topic: "ignorecollision"
Viewing all articles
Browse latest Browse all 148

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

$
0
0
Hello!

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); Debug.Log("test1"); } //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.

Viewing all articles
Browse latest Browse all 148

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>