Search Unity

Question Collisions are not detected once object with RigidBody becomes a child of another object

Discussion in 'Physics' started by bon_ich, Dec 28, 2023.

  1. bon_ich

    bon_ich

    Joined:
    Aug 10, 2020
    Posts:
    2
    Hi all,
    have a problem and can't figure it out.
    I have:
    • moving platform
    • character controller player object
    • box object which is destroyed on collision with player
    Both box and player become platform children to move along with it. But once box becomes platform child it stops detecting a collistion with player.
    This is collision detection code from the box script:
    Code (CSharp):
    1. private void OnCollisionEnter(Collision other)
    2.     {
    3.         if (other.collider.TryGetComponent(out PlayerController player))
    4.         {
    5.             Collected.Invoke();
    6.             GameManager.Instance.CollectedBoxesCount++;
    7.             Destroy(gameObject);
    8.         }
    9.         if (other.gameObject.GetComponentInParent<MovingPlatform>() != null)
    10.         {
    11.             transform.parent = other.transform;
    12.         }
    13.     }
    upload_2023-12-28_23-49-20.png

    Please let me know what am I doing wrong.
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    OnCollisionEnter doesn't work with the character controller. Instead you can use OnTriggerEnter and add a second collider to the box and make the second collider the trigger. Also make the trigger collider bigger than the box to make sure it can be touched/triggered.
     
  3. bon_ich

    bon_ich

    Joined:
    Aug 10, 2020
    Posts:
    2
    thank you! will try