How to prevent colliders from passing through each other?

后端 未结 8 1411
天命终不由人
天命终不由人 2020-12-04 20:23

I am having trouble keeping game objects inside of a contained space. When they reach the edge, there is some momentary push back but then they will go right through the wal

8条回答
  •  独厮守ぢ
    2020-12-04 21:01

    So I haven't been able to get the Mesh Colliders to work. I created a composite collider using simple box colliders and it worked exactly as expected.

    Other tests with simple Mesh Colliders have come out the same.

    It looks like the best answer is to build a composite collider out of simple box/sphere colliders.

    For my specific case I wrote a Wizard that creates a Pipe shaped compound collider.

    @script AddComponentMenu("Colliders/Pipe Collider");
    class WizardCreatePipeCollider extends ScriptableWizard
    {
        public var outterRadius : float = 200;
        public var innerRadius : float = 190;
        public var sections : int = 12;
        public var height : float = 20;
    
        @MenuItem("GameObject/Colliders/Create Pipe Collider")
        static function CreateWizard()
        {
            ScriptableWizard.DisplayWizard.("Create Pipe Collider");
        }
    
        public function OnWizardUpdate() {
            helpString = "Creates a Pipe Collider";
        }
    
        public function OnWizardCreate() {
            var theta : float = 360f / sections;
            var width : float = outterRadius - innerRadius;
    
            var sectionLength : float = 2 * outterRadius * Mathf.Sin((theta / 2) * Mathf.Deg2Rad);
    
            var container : GameObject = new GameObject("Pipe Collider");
            var section : GameObject;
            var sectionCollider : GameObject;
            var boxCollider : BoxCollider;
    
            for(var i = 0; i < sections; i++)
            {
                section = new GameObject("Section " + (i + 1));
    
                sectionCollider = new GameObject("SectionCollider " + (i + 1));
                section.transform.parent = container.transform;
                sectionCollider.transform.parent = section.transform;
    
                section.transform.localPosition = Vector3.zero;
                section.transform.localRotation.eulerAngles.y = i * theta;
    
                boxCollider = sectionCollider.AddComponent.();
                boxCollider.center = Vector3.zero;
                boxCollider.size = new Vector3(width, height, sectionLength);
    
                sectionCollider.transform.localPosition = new Vector3(innerRadius + (width / 2), 0, 0);
            }
        }
    }
    

提交回复
热议问题