Search Unity

How to change object's rigidbody during runtime?

Discussion in '2D' started by Deku42, Feb 21, 2020.

  1. Deku42

    Deku42

    Joined:
    Feb 5, 2020
    Posts:
    5
    Hello! I am attempting to code it so that when my player collides with a shape (triangle, square, circle, etc.), the player's sprite and actual COLLIDER (rigidbody) shape changes. Changing its sprite is easy enough, but how do I make it so my player inherits the rigidbody (and thus collision shape) of another object? Is this possible WITHOUT creating another object?

    Thank you!
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    not sure but how many shape collider you need? if small can do manually
     
  3. Deku42

    Deku42

    Joined:
    Feb 5, 2020
    Posts:
    5
    Actually just 3! Just the triangle square and circle.
     
  4. ShiroTheWhite

    ShiroTheWhite

    Joined:
    Apr 4, 2018
    Posts:
    16
    You could add all three colliders to the gameobject but disable two of them. Then when you collide with the other object disable the currently enabled collider then enable one of the other colliders on the gameobject

    for example

    BoxCollider2D mBCollider;
    CapsuleCollider2D mCCollider;
    CircleCollider2D mCircleCollider;

    void Start {
    mBCollider = GetComponent<BoxCollider2D>();
    mCCollider = GetComponent<CircleCollider2D>();
    mCircleCollider = GetComponent<CapsuleCollider2D>();
    }

    void EnableCollider(int id)
    {
    if(id==0) { mBCollider.enabled = true; mCCollider.enabled = true; mCircleCollider.enabled = true; }
    else if(id==1) { mBCollider.enabled = false; mCCollider.enabled = true; mCircleCollider.enabled =false;}
    else if(id==2) {mBCollider.enabled = false; mCCollider.enabled = false; mCircleCollider.enabled = true; }
    }

    then just call EnableCollider(<number to enable>) to enable it on collision or something

    for example to enable CircleCollider you do -> EnableCollider(1)
     
    Deku42 likes this.
  5. Deku42

    Deku42

    Joined:
    Feb 5, 2020
    Posts:
    5
    Thanks much! I did not know you could add multiple colliders to the same object. You helped a lot. +1!