Search Unity

GetComponentsInChildren<Collider2D>

Discussion in 'Scripting' started by S3Critsss, Jan 21, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I want my character as a ragdoll but when i try to Ignore the colliders it doesnt work
    this is my movement script
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class movePlayer : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.     public float speed;
    9.     public float jump;
    10.     private float move;
    11.     private Rigidbody2D rb;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponentInChildren<Rigidbody2D>();
    16.  
    17.         Collider2D[] colliders = Player.GetComponentsInChildren<Collider2D>();
    18.         for (int i = 0; i < colliders.Length; i++)
    19.         {
    20.             for (int k = i + 1; k < colliders.Length; k++)
    21.             {
    22.                 Physics2D.IgnoreCollision(colliders[i], colliders[k]);
    23.             }
    24.         }
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         move = Input.GetAxis("Horizontal");
    30.  
    31.         rb.velocity = new Vector2(move * speed, rb.velocity.y);
    32.     }
    33. }
    34.  
    and my heirarchy for player is 1.png