Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to move BoxCollider2D?

Discussion in 'Physics' started by AnthonySturdy, Mar 1, 2015.

  1. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Hi, I want to move a BoxCollider2D to a certain Position relative to my Player, but whenever I use something like
    BoxCollider2D.transform.position it moves the player and not the collider. I'm using a Non-kinematic rigidbody aswell.if that helps.
    Thanks, Anthony.
     
  2. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Well, you do not.
    Any game object with only a collider attached is considered static, and you are not supposed to move an static collider.
    But, if you really want to move your object, add a rigidbody and move it with physics. Or attach a rigidbody, check "is kinematic", and move changing its transform.

    Also, why BoxCollider2D.transform.position?
    BoxCollider2D is a class, not an object. You should try transform.position or rigidbody.AddForce.

    Good luck.
     
  3. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    It's not the player I want to move, I have that. I want to move the BoxCollider2D which is attached to the player, depending on what button they press :)
     
  4. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Then change the center.
    Code (JavaScript):
    1. //If you have more than just one collider, assign them on the Inspector
    2.  
    3. myBoxCollider2D.center.x = someValueForX;
    4. myBoxCollider2D.center.y = someValueForY;
     
  5. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Great, thanks a bunch :D